Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 848 Bytes

67.md

File metadata and controls

46 lines (35 loc) · 848 Bytes

Add Binary

Description

link


Solution

See Code


Code

O(m * m)

class Solution:
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        if a == '':return b
        if b == '':return a
        if a[-1] == '1' and b[-1] == '1':
            return self.addBinary(self.addBinary(a[:-1], b[:-1]), '1') + '0'
        if a[-1] == '0' and b[-1] == '0':
            return self.addBinary(a[:-1], b[:-1]) + '0'
        else:
            return self.addBinary(a[:-1], b[:-1]) + '1'


class Solution:
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        summed = int('0b' + a, 2) + int('0b' + b, 2)
        return bin(summed)[2:]