Skip to content

Instantly share code, notes, and snippets.

@munguial
Created July 19, 2020 20:35
Show Gist options
  • Save munguial/d5a3ba97fdbd68a4642a08aeb37bc85c to your computer and use it in GitHub Desktop.
Save munguial/d5a3ba97fdbd68a4642a08aeb37bc85c to your computer and use it in GitHub Desktop.
July - Day 19 - Add Binary
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3395/
class Solution(object):
def addBinary(self, a: str, b: str) -> str:
return self.add(a, b, len(a) - 1, len(b) - 1, 0)
def add(self, a: str, b: str, i: int, j: int, c: int):
if c == 0 and i < 0 and j < 0:
return ''
v1 = int(a[i]) if i >= 0 else 0
v2 = int(b[j]) if j >= 0 else 0
current = v1 ^ v2 ^ c
c = (v1 & v2) | ((v1 ^ v2) & c)
return self.add(a, b, i - 1, j - 1, c) + str(current)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment