Skip to content
RED SAGSkills
easy15 ptsstringsmath

Add Two Binary Strings

Two non-negative integers are given as binary strings a and b (no leading zeros except the number "0" itself). Return their sum as a binary string.

The strings can be much longer than 53 bits, so do the addition digit by digit.

Example 1

Input: a = "11", b = "1"
Output: "100"

Example 2

Input: a = "1010", b = "1011"
Output: "10101"

Constraints

  • 1 <= a.length, b.length <= 10^4
  • a and b contain only 0 and 1.
Hint 1

Walk both strings from the right end, carrying like long addition.

Hint 2

Collect digits in an array and reverse once at the end.

Good practice for: Embedded Systems Engineer, Game Developer, Backend Developer, Blockchain Developer, Frontend Developer

Run checks your code on 2 sample tests. Submit runs all 6 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.