Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = “11”, b = “1”

Output: “100”

Example 2:

Input: a = “1010”, b = “1011”

Output: “10101”

Solution

from typing import List

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        return f"{int(a, 2) + int(b, 2):b}"

Test Cases

test = Solution()
answer = test.addBinary("11", "1")
assert answer == "100"
answer = test.addBinary("1010", "1011")
assert answer == "10101"
answer = test.addBinary("0", "0")
assert answer == "0"
answer = test.addBinary("1111", "1111")
assert answer == "11110"
print('All Passed!')

Big O Analysis

Space Complexity: O(1)

Time Complexity: O(1)