Add Binary - Problem
Given two binary strings a and b, return their sum as a binary string.
A binary string contains only characters '0' and '1'. You need to perform binary addition following the same rules as decimal addition, but in base 2.
Binary Addition Rules:
0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 10(0 with carry 1)
Input & Output
Example 1 — Basic Addition
$
Input:
a = "11", b = "1"
›
Output:
"100"
💡 Note:
Binary addition: 11₂ + 1₂ = 100₂ (which is 3 + 1 = 4 in decimal)
Example 2 — Different Lengths
$
Input:
a = "1010", b = "1011"
›
Output:
"10101"
💡 Note:
Binary addition: 1010₂ + 1011₂ = 10101₂ (which is 10 + 11 = 21 in decimal)
Example 3 — Single Digit
$
Input:
a = "1", b = "1"
›
Output:
"10"
💡 Note:
Binary addition: 1₂ + 1₂ = 10₂ (which is 1 + 1 = 2 in decimal)
Constraints
- 1 ≤ a.length, b.length ≤ 104
- a and b consist only of '0' or '1' characters
- Each string does not contain leading zeros except for the zero itself
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two binary strings: "1011" and "1"
2
Process
Add bit by bit from right to left with carry
3
Output
Result binary string: "1100"
Key Takeaway
🎯 Key Insight: Binary addition follows the same carry logic as decimal, but carries occur when sum ≥ 2
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code