""" 0067.1 - Add Binary - Solution 1 - Iterative Bit-by-Bit Addition """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def addBinary(self, a: str, b: str) -> str:
"""Add Binary Function"""
result = []
carry = 0
i, j = len(a) - 1, len(b) - 1
while i >= 0 or j >= 0 or carry:
total = carry
if i >= 0:
total += int(a[i])
i -= 1
if j >= 0:
total += int(b[j])
j -= 1
result.append(str(total % 2))
carry = total // 2
return ''.join(reversed(result))
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().addBinary("11", "1")) # "100"
print(Solution().addBinary("1010", "1011")) # "10101"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0067.2 - Add Binary - Solution 2 - Stack-Based Addition with Pop """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def addBinary(self, a: str, b: str) -> str:
"""Add Binary Function"""
a, b = list(a), list(b)
carry = 0
result = ""
while a or b or carry:
if a:
carry += int(a.pop())
if b:
carry += int(b.pop())
result = str(carry % 2) + result
carry //= 2
return result
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().addBinary("11", "1")) # "100"
print(Solution().addBinary("1010", "1011")) # "10101"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()