Add Binary

Given two binary strings a and b, return their sum as a binary string.

The input strings represent non-negative integers in base 2. You should add them using binary addition and return the resulting binary representation as a string.

Example 1
Inputa = "11", b = "1"
Output"100"
11 is 3 and 1 is 1, so their sum is 4, which is 100 in binary.
Example 2
Inputa = "1010", b = "1011"
Output"10101"
1010 is 10 and 1011 is 11, so their sum is 21, which is 10101 in binary.

Constraints

  • 1 <= a.length, b.length <= 10^4
  • a and b consist only of '0' or '1' characters
  • Each string does not contain leading zeros except for the zero itself

Asked at 13 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate