class Solution: def addBinary(self, a: str, b: str) -> str: a = list(a) b = list(b) re = [] # save result rd = 0 # round while a and b: s = rd + int(a.pop()) + int(b.pop()) if s > 1: re.append(str(s-2)) rd = 1 else: re.append(str(s)) rd = 0 c = a if a else b while c: s = rd + int(c.pop()) if s > 1: re.append(str(s-2)) rd = 1 else: re.append(str(s)) rd = 0 if rd == 1: re.append('1') re.reverse() return ''.join(re) def main(): s = Solution() print(s.addBinary("11", "1")) # 100 print(s.addBinary("1010", "1011")) # 10101 if __name__ == '__main__': main()