Problem
Given two binary strings a
and b
, return their sum as a binary string.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Start From End and Take the Carry
Here is the approach:
-
Initialize a StringBuilder:
- Use a
StringBuilder
to construct the result string.
- Use a
-
Initialize Pointers and Carry:
- Use two pointers
i
andj
to traverse the binary strings from the end towards the beginning. - Use a variable
carry
to keep track of the carry-over during the addition process.
- Use two pointers
-
Digit-by-Digit Addition:
- Iterate through both strings from the end to the beginning.
- Add corresponding digits from both strings along with the carry.
- Append the result of the addition modulo 2 (i.e., the sum’s last binary digit) to the
StringBuilder
. - Update the carry to be the integer division of the sum by 2.
-
Handle Remaining Carry:
- After exiting the loop, check if there is any remaining carry and append it to the result.
-
Reverse the StringBuilder:
- Since the result is constructed in reverse order, reverse the
StringBuilder
at the end to get the correct binary sum.
- Since the result is constructed in reverse order, reverse the
Very simple, nothing special. Note how to convert a character to an int. Two pointers starting from the back, just think of adding two regular ints from you add from back
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(max(m, n))
, wherem
andn
are the lengths of the binary stringsa
andb
. The algorithm processes each digit of the longer string once. - 🧺 Space complexity:
O(max(m, n))
for storing the output in theStringBuilder
.
Method 2 - Use Xor for Digit and Mod for Carry
To add two binary strings a
and b
, the approach involves simulating the digit-by-digit addition process from the least significant digit to the most significant digit, similar to manual binary addition.
Here is the approach:
- Handle Null or Empty Strings:
- If either string
a
orb
is null or empty, return the other string as the result.
- If either string
- Convert Strings to Character Arrays:
- Convert the binary strings
a
andb
to character arrays for easy manipulation.
- Convert the binary strings
- Initialization:
- Initialize pointers
i
andj
to point to the last characters of the arraysaArray
andbArray
. - Initialize variables
aByte
,bByte
,carry
, andresult
for the addition process.
- Initialize pointers
- Digit-by-Digit Addition:
- Use a
while
loop to iterate through both arrays from the end to the beginning and perform binary addition. - Use bitwise XOR to calculate the sum of the current digits and the carry.
- Update the carry for the next iteration based on the sum of the current digits.
- Append the result of the current addition to the
StringBuilder
.
- Use a
- Reverse and Return:
- Reverse the
StringBuilder
to obtain the correct order of the resultant binary string. - Return the resultant string.
- Reverse the
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(max(m, n))
, wherem
andn
are the lengths of the binary stringsa
andb
. This is because we process each digit of the longer string once. - 🧺 Space complexity:
O(max(m, n))
for storing the output in theStringBuilder
.