Skip to content

Latest commit

 

History

History
42 lines (37 loc) · 1.04 KB

README.md

File metadata and controls

42 lines (37 loc) · 1.04 KB

67. Add Binary

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"

Solutions (Python)

1. Solution

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        i = -1
        c = '0'
        ret = ""
        while i >= -len(a) or i >= -len(b):
            if (i < -len(a) or a[i] == '0') and (i < -len(b) or b[i] == '0'):
                ret = c + ret
                c = '0'
            elif i >= -len(a) and a[i] == '1' and i >= -len(b) and b[i] == '1':
                ret = c + ret
                c = '1'
            elif c == '0':
                ret = '1' + ret
            else:
                ret = '0' + ret
            i -= 1
        if c == '1':
            ret = '1' + ret
        return ret