Advertisement
smj007

Untitled

Mar 16th, 2025
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. class Solution:
  2.     def addBinary(self, a: str, b: str) -> str:
  3.        
  4.         # first convert to numbers
  5.         x = int(a, 2)
  6.         y = int(b, 2)
  7.  
  8.         while y:
  9.             answer_without_carry = x^y # yaad karo
  10.             carry = (x&y) << 1 # AND operator and left shift        
  11.             # now you need to keep doing untill there is no carry left
  12.             x, y = answer_without_carry, carry
  13.  
  14.         return bin(x)[2:]
  15.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement