Add Two Binary Strings
Solve this Problema and b, return their sum, also as a binary string.
This is elementary-school addition, just in base 2 instead of base 10 — walk both strings from the rightmost digit, add corresponding digits plus any carry from the previous position, and keep the overflow as a carry into the next position. The only real design decision is HOW to build the result string as you go: strings are immutable, so repeatedly gluing a new digit onto the FRONT forces a full copy every time. Appending to the end instead — then reversing once at the finish — does the same amount of real work in a fraction of the total time.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ a.length, b.length ≤ 10⁴ - ◆
a and b consist only of the characters '0' or '1' - ◆
Neither a nor b has leading zeros, except the string "0" itself
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public String addBinary(String a, String b) { |
| 3 | StringBuilder result = new StringBuilder(); |
| 4 | int i = a.length() - 1, j = b.length() - 1, carry = 0; |
| 5 | while (i >= 0 || j >= 0 || carry > 0) { |
| 6 | int sum = carry; |
| 7 | if (i >= 0) sum += a.charAt(i--) - '0'; |
| 8 | if (j >= 0) sum += b.charAt(j--) - '0'; |
| 9 | result.append(sum % 2); |
| 10 | carry = sum / 2; |
| 11 | } |
| 12 | return result.reverse().toString(); |
| 13 | } |
| 14 | } |
| 15 |
330Start at the rightmost digit of both strings (i = 3, j = 3), with carry = 0 and an empty buffer to append into.
Approach & Solutions
Brute Force — Prepend to a Growing String
BruteWalk both strings from the rightmost digit, tracking a carry. At each position, compute the digit sum and PREPEND the resulting bit to the front of the result string. Correct, but strings are immutable — every prepend allocates a brand-new string and copies everything built so far, so the total cost adds up to O(n²).
O(n²)O(n)1class Solution {
2 public String addBinary(String a, String b) {
3 int i = a.length() - 1, j = b.length() - 1, carry = 0;
4 String result = "";
5 while (i >= 0 || j >= 0 || carry > 0) {
6 int sum = carry;
7 if (i >= 0) sum += a.charAt(i--) - '0';
8 if (j >= 0) sum += b.charAt(j--) - '0';
9 result = (sum % 2) + result;
10 carry = sum / 2;
11 }
12 return result;
13 }
14}Optimal — StringBuilder Append + Reverse
OptimalSame digit-by-digit walk with a carry, but APPEND each new bit to the end of a mutable buffer instead of prepending — appending is O(1) amortized, since nothing already in the buffer needs to move. That builds the digits in reverse order, so reverse the whole buffer once at the end. One O(n) reversal is far cheaper than n prepends.
O(n)O(n)1class Solution {
2 public String addBinary(String a, String b) {
3 StringBuilder result = new StringBuilder();
4 int i = a.length() - 1, j = b.length() - 1, carry = 0;
5 while (i >= 0 || j >= 0 || carry > 0) {
6 int sum = carry;
7 if (i >= 0) sum += a.charAt(i--) - '0';
8 if (j >= 0) sum += b.charAt(j--) - '0';
9 result.append(sum % 2);
10 carry = sum / 2;
11 }
12 return result.reverse().toString();
13 }
14}