Skip to content

Commit

Permalink
Created Leetcode67.py
Browse files Browse the repository at this point in the history
Another algo to add two binaries given as strings
  • Loading branch information
VikasHanumegowda authored Sep 21, 2017
1 parent 5e77170 commit 3d49aa1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions leetcode67.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
'''
class Solution(object):
def helper(self, *args):
count = 0
empty = 0
for x in args:
if len(x)>0:
if x[-1] == '1':
count+=1
else:
empty = 1
args = list(args)
while [] in args:
args.remove([])
args = tuple(args)
#print(args)
if len(args) == 0:
return ""
args = [x[:-1] for x in args]
if count == 0:
return self.helper(*args)+'0'
if count ==1:
return self.helper(*args)+'1'
if count == 2:
args.append(['1'])
return self.helper(*args)+'0'
if count == 3:
args.append(['1'])
return self.helper(*args)+'1'
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
return self.helper([x for x in a],[x for x in b])
# print(Solution().addBinary("101",'11'))

0 comments on commit 3d49aa1

Please sign in to comment.