/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 or 1 characters and do not have leading zeros except for the zero itself.

Example 1

Input: binary_str1 = 1100, binary_str2 = 101

Output: 10001

Explanation: 1100 (12) + 101 (5) = 10001 (17)

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), len(binary_str2) <= 10_000
  • binary_str1 and binary_str2 contain only 0 or 1 characters
  • No leading zeros in binary_str1 or binary_str2 except for the string 0
Python (current runtime)

Case 1

Input: binary_str1 = '1001', binary_str2 = '110'

Expected: '1111'

Case 2

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

Expected: '100000'

Case 3

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

Expected: '101010'

Case 4

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

Expected: '1'

Case 5

Input: binary_str1 = '100000', binary_str2 = '100000'

Expected: '1000000'