Skip to content

Commit 0d6a793

Browse files
Bit Manipulation: 67. Add Binary
1 parent f5be1f7 commit 0d6a793

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* [260](https://leetcode.com/problems/single-number-iii/). Single Number III - [code](src/bit_manipulation/easy/TwoSingleNumbers.java)
5454
* [405](https://leetcode.com/problems/convert-a-number-to-hexadecimal/). Convert a Number to Hexadecimal - [code](src/bit_manipulation/easy/ConvertANumberToHexadecimal.java)
5555
* [268](https://leetcode.com/problems/missing-number/). Missing Number - [code](src/bit_manipulation/easy/MissingNumber.java)
56+
* [67](https://leetcode.com/problems/add-binary/). Add Binary - [code](src/bit_manipulation/easy/AddBinary.java)
5657
* [Gfg](https://practice.geeksforgeeks.org/problems/check-whether-k-th-bit-is-set-or-not-1587115620/1). Check whether K-th bit is set or not - [code](src/bit_manipulation/easy/CheckKthBitIsSet.java)
5758
* [Gfg](https://practice.geeksforgeeks.org/problems/odd-or-even3618/1). Odd or Even - [code](src/bit_manipulation/easy/OddOrEven.java)
5859
* [Gfg](https://practice.geeksforgeeks.org/problems/set-kth-bit3724/1). Set kth bit - [code](src/bit_manipulation/easy/SetKthBit.java)
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package bit_manipulation.easy;
2+
3+
/***
4+
* Problem 67 in Leetcode: https://leetcode.com/problems/add-binary/
5+
*
6+
* Given two binary strings a and b, return their sum as a binary string.
7+
*
8+
* Example 1:
9+
* Input: a = "1001010101011010101010110101111", b = "1010010101001010101010"
10+
* Output: "1001010110101101010100001011001"
11+
*
12+
* Example 2:
13+
* Input: a = "1010", b = "1011"
14+
* Output: "10101"
15+
*/
16+
17+
public class AddBinary {
18+
public static void main(String[] args) {
19+
String a = "1001010101011010101010110101111", b = "1010010101001010101010";
20+
21+
System.out.println("Brute Force: " + addBinaryBruteForce(a, b));
22+
System.out.println("Optimized: " + addBinaryOptimized(a, b));
23+
}
24+
25+
private static String addBinaryBruteForce(String a, String b) {
26+
int n = a.length() - 1;
27+
int m = b.length() - 1;
28+
char carry = '0';
29+
StringBuilder sb = new StringBuilder();
30+
31+
while ((n >= 0) && (m >= 0)) {
32+
char bit1 = a.charAt(n);
33+
char bit2 = b.charAt(m);
34+
35+
if ((bit1 == '1') && (bit2 == '1')) {
36+
if (carry == '1') {
37+
sb.append('1');
38+
} else {
39+
sb.append('0');
40+
carry = '1';
41+
}
42+
} else if ((bit1 == '0') && (bit2 == '0')) {
43+
if (carry == '1') {
44+
sb.append('1');
45+
carry = '0';
46+
} else {
47+
sb.append('0');
48+
}
49+
} else {
50+
if (carry == '1') {
51+
sb.append('0');
52+
} else {
53+
sb.append('1');
54+
}
55+
}
56+
n--;
57+
m--;
58+
}
59+
60+
while (n >= 0) {
61+
char bit = a.charAt(n);
62+
if (carry == '1') {
63+
if (bit == '1') {
64+
sb.append('0');
65+
} else {
66+
sb.append('1');
67+
carry = '0';
68+
}
69+
} else {
70+
sb.append(bit);
71+
}
72+
n--;
73+
}
74+
75+
while (m >= 0) {
76+
char bit = b.charAt(m);
77+
if (carry == '1') {
78+
if (bit == '1') {
79+
sb.append('0');
80+
} else {
81+
sb.append('1');
82+
carry = '0';
83+
}
84+
} else {
85+
sb.append(bit);
86+
}
87+
m--;
88+
}
89+
90+
if (carry == '1') {
91+
sb.append('1');
92+
}
93+
94+
return sb.reverse().toString();
95+
}
96+
97+
private static String addBinaryOptimized(String a, String b) {
98+
int n = a.length() - 1;
99+
int m = b.length() - 1;
100+
int carry = 0;
101+
StringBuilder sb = new StringBuilder();
102+
103+
while ((n >= 0) || (m >= 0)) {
104+
int sum = 0;
105+
if ((n >= 0) && (a.charAt(n) == '1')) {
106+
sum++;
107+
}
108+
if ((m >= 0) && (b.charAt(m) == '1')) {
109+
sum++;
110+
}
111+
sum += carry;
112+
if (sum >= 2) {
113+
carry = 1;
114+
} else {
115+
carry = 0;
116+
}
117+
sb.append(sum % 2);
118+
n--;
119+
m--;
120+
}
121+
122+
if (carry == 1) {
123+
sb.append('1');
124+
}
125+
126+
return sb.reverse().toString();
127+
}
128+
}

0 commit comments

Comments
 (0)