/67. Add Binary

67. Add Binary

Easy
Probability57.8% acceptance

Given two strings representing binary numbers, compute their sum and return the result as a binary string. Both input strings contain only 0 and 1 characters and do not have leading zeros except for the zero itself.

Example 1

Input: binary_str1=1101, binary_str2=101

Output: 10010

Explanation: 1101 (13) + 101 (5) = 10010 (18)

Example 2

Input: binary_str1=0, binary_str2=0

Output: 0

Explanation: 0 + 0 = 0

Example 3

Input: binary_str1=111, binary_str2=111

Output: 1110

Explanation: 111 (7) + 111 (7) = 1110 (14)

Constraints

  • 1 <= len(binary_str1) <= 104
  • 1 <= len(binary_str2) <= 104
  • binary_str1 and binary_str2 consist only of 0 or 1 characters
  • Neither string contains leading zeros except for the zero itself
Python (current runtime)

Case 1

Input: binary_str1='100', binary_str2='1100'

Expected: '1000'

Case 2

Input: binary_str1='1', binary_str2='1111'

Expected: '10000'

Case 3

Input: binary_str1='10101', binary_str2='10'

Expected: '10111'

Case 4

Input: binary_str1='1000', binary_str2='1000'

Expected: '10000'

Case 5

Input: binary_str1='111111', binary_str2='1'

Expected: '1000000'