Skip to content

Commit

Permalink
added problem & solution 67.AddBinary
Browse files Browse the repository at this point in the history
  • Loading branch information
aktienautobahn committed Sep 2, 2024
1 parent dc7a955 commit 280d10f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Easy/67.AddBinary/67.AddBinary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <string>
#include <vector>
#include <algorithm>


class Solution {
public:
std::string addBinary(std::string a, std::string b) {
// full adder with binary logic on strings
// iterate from the last element to the first one
// and pass the carry to the higher (to the left) position if needed.
std::vector<bool> outputVector;
std::string result;
int maxLen = std::max(a.size(), b.size());
int i = a.size() - 1; // last element of a
int j = b.size() - 1; // last element of b
bool sum = false;
bool carryIn = false;
bool aBool = false;
bool bBool = false;

while (maxLen > 0) {
// if both elements exist case
if (i >= 0 && j >= 0) {
// convert char to bit
aBool = (a[i] - '0') != 0;
bBool = (b[j] - '0') != 0;
i--;
j--;
}
// only one element exist case
else {
if (i >= 0) {
// convert char to bit
aBool = (a[i] - '0') != 0;
bBool = false;
i--;
} else if (j >= 0) { // j > 0
aBool = false;
bBool = (b[j] - '0') != 0;
j--;
}

}
// bitweise logical ops
sum = aBool ^ bBool ^ carryIn;
carryIn = (aBool & bBool) | (carryIn & (aBool ^ bBool));
// Append the current sum to the result string
result.push_back(sum ? '1' : '0');
maxLen--;
}
// last carryIn insertion
if (carryIn) {
result.push_back(carryIn ? '1' : '0');
}
std::reverse(result.begin(), result.end());
return result;
}

};

int main() {
std::string stringA = "11";
std::string stringB = "1";

Solution obj;
obj.addBinary(stringA, stringB);
return 0;
}
19 changes: 19 additions & 0 deletions Easy/67.AddBinary/67.AddBinary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## 67. **Add Binary**
- **Problem statement:** Given two binary strings `a` and `b`, return their sum as a binary string.
- **Example 1:**
- **Input:** `a = "11"`, `b = "1"`
- **Output:** `"100"`
- **Example 2:**
- **Input:** `a = "1010"`, `b = "1011"`
- **Output:** `"10101"`
- **Constraints:**
- `1 <= a.length, b.length <= 10^4`
- `a` and `b` consist only of '0' or '1' characters.
- Each string does not contain leading zeros except for the zero itself.



## Solution:
Implement a full-adder logic to sum the binary strings. Start iterating from the least significant bit (end of the strings) and move towards the most significant bit, managing the carry. Append the result to a string and reverse it at the end to get the final binary sum.
**Time Complexity:** `O(n)` – Where `n` is the length of the longer binary string. Each bit is processed once in a single pass through the strings.
**Space Complexity:** `O(n)` – Additional space is used for the result string, which is proportional to the length of the input strings.
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ Welcome to my repository where I showcase my journey in mastering Data Structure
- **Approach:** Iterate from the least significant digit to the most significant, handling carry-over. Insert a new digit at the beginning if a carry affects the most significant digit.
[View Solution](./Easy/66.PlusOne/66.PlusOne.cpp)

13. **AddBinary**
- **Problem statement:** Given two binary strings `a` and `b`, return their sum as a binary string.
- **Approach:** Implement a full-adder logic to sum the binary strings. Start iterating from the least significant bit (end of the strings) and move towards the most significant bit, managing the carry. Append the result to a string and reverse it at the end to get the final binary sum.
[View Solution](./Easy/67.AddBinary/67.AddBinary.cpp)

## 🛠️ Tools & Technologies

- **Language:** C++
Expand Down

0 comments on commit 280d10f

Please sign in to comment.