Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

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"

My code:

class Solution:
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        self.result=""
        self.add_digit(a,b,len(a)-1,len(b)-1,False)
        return self.result
    def add_digit(self,a,b,i,j,add_label):
        if i<0 and j<0:
            if add_label:
                self.result='1'+self.result
            return
        if i<0:
            for k in range(j,-1,-1):
                if add_label and b[k]=='1':
                    #b[k]='0'
                    add_label=True
                    self.result='0'+self.result
                    continue
                if add_label:
                    #b[k]='1'
                    add_label=False
                    self.result='1'+self.result
                    continue
                self.result=b[k]+self.result
            if add_label:
                self.result='1'+self.result
            return
        if j<0:
            #print(self.result)
            #print(i)
            #print(add_label)
            for k in range(i,-1,-1):
                #print(k)
                if add_label and a[k]=='1':
                    #a[k]='0'
                    add_label=True
                    self.result='0'+self.result
                    continue
                if add_label:
                    #a[k]='1'
                    add_label=False
                    self.result='1'+self.result
                    continue
                self.result=a[k]+self.result
            if add_label:
                self.result='1'+self.result
            #print(self.result)
            return
        if a[i]=='0' and b[j]=='0':
            if add_label:
                self.result='1'+self.result
            else:
                self.result='0'+self.result
            self.add_digit(a,b,i-1,j-1,False)
        elif ((a[i]=='1' and b[j]=='0') or (a[i]=='0' and b[j]=='1')) and not add_label:
            self.result='1'+self.result
            self.add_digit(a,b,i-1,j-1,False)
        elif ((a[i]=='1' and b[j]=='0') or (a[i]=='0' and b[j]=='1')) and add_label:
            self.result='0'+self.result
            self.add_digit(a,b,i-1,j-1,True)
        else:
            if add_label:
                self.result='1'+self.result
            else:
                self.result='0'+self.result
            self.add_digit(a,b,i-1,j-1,True)
            
        

Solution: https://leetcode.com/problems/add-binary/