Skip to content

Commit 8b135b5

Browse files
Added solution for Add Binary
1 parent 5e2d714 commit 8b135b5

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
id: add-binary
3+
title: Add Binary (LeetCode)
4+
difficulty: Easy
5+
sidebar_label: 0067-AddBinary
6+
topics:
7+
- Math
8+
- String
9+
---
10+
11+
## Problem Description
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :---------------- | :------------ | :--------------- |
15+
| [Merge Two Sorted Lists](https://leetcode.com/problems/add-binary/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/add-binary/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
16+
17+
## Problem Description
18+
19+
Given two binary strings `a` and `b`, return their sum as a binary string.
20+
21+
### Examples
22+
23+
#### Example 1:
24+
25+
- **Input:** `a = "11"`, `b = "1"`
26+
- **Output:** `"100"`
27+
- **Explanation:** The sum of binary strings `11` and `1` is `100`.
28+
29+
#### Example 2:
30+
31+
- **Input:** `a = "1010"`, `b = "1011"`
32+
- **Output:** `"10101"`
33+
- **Explanation:** The sum of binary strings `1010` and `1011` is `10101`.
34+
35+
### Constraints:
36+
37+
- `1 <= a.length, b.length <= 10^4`
38+
- `a` and `b` consist only of `'0'` or `'1'` characters.
39+
- Each string does not contain leading zeros except for the zero itself.
40+
41+
### Approach
42+
43+
To add two binary strings `a` and `b`, we can follow these steps:
44+
45+
1. Initialize variables `result` and `carry` to store the result and carry, respectively.
46+
2. Start from the last digit of both strings and move towards the beginning.
47+
3. Add the corresponding digits from `a` and `b` along with the carry and update the result accordingly.
48+
4. If the sum exceeds 1, set the carry to 1; otherwise, set it to 0.
49+
5. After processing all digits, if there's still a carry left, append it to the result.
50+
6. Reverse the result string to get the final sum.
51+
52+
### Solution Code
53+
54+
#### Python
55+
56+
```
57+
class Solution(object):
58+
def addBinary(self, a, b):
59+
return bin(int(a, 2) + int(b, 2))[2:]
60+
```
61+
62+
#### C++
63+
64+
```
65+
class Solution {
66+
public:
67+
string addBinary(string a, string b) {
68+
string result;
69+
int carry = 0;
70+
int i = a.length() - 1, j = b.length() - 1;
71+
72+
while (i >= 0 || j >= 0) {
73+
int digit_a = (i >= 0) ? a[i] - '0' : 0;
74+
int digit_b = (j >= 0) ? b[j] - '0' : 0;
75+
int current_sum = digit_a + digit_b + carry;
76+
result = to_string(current_sum % 2) + result;
77+
carry = current_sum / 2;
78+
i--;
79+
j--;
80+
}
81+
82+
if (carry) {
83+
result = "1" + result;
84+
}
85+
86+
return result;
87+
}
88+
};
89+
```
90+
91+
#### Java
92+
93+
```
94+
class Solution {
95+
public String addBinary(String a, String b) {
96+
StringBuilder result = new StringBuilder();
97+
int carry = 0;
98+
int i = a.length() - 1, j = b.length() - 1;
99+
100+
while (i >= 0 || j >= 0) {
101+
int digit_a = (i >= 0) ? a.charAt(i) - '0' : 0;
102+
int digit_b = (j >= 0) ? b.charAt(j) - '0' : 0;
103+
int current_sum = digit_a + digit_b + carry;
104+
result.insert(0, current_sum % 2);
105+
carry = current_sum / 2;
106+
i--;
107+
j--;
108+
}
109+
110+
if (carry == 1) {
111+
result.insert(0, '1');
112+
}
113+
114+
return result.toString();
115+
}
116+
}
117+
```
118+
119+
### Conclusion
120+
121+
The "Add Binary" problem can be efficiently solved by adding two binary strings digit by digit and considering the carry. The provided solution code implements this approach in Python, C++, and Java, providing an optimal solution to the problem.

0 commit comments

Comments
 (0)