-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added problem & solution 67.AddBinary
- Loading branch information
1 parent
dc7a955
commit 280d10f
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters