-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_binary_67.py
More file actions
35 lines (33 loc) · 883 Bytes
/
Copy pathadd_binary_67.py
File metadata and controls
35 lines (33 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Problem: Add Binary
LeetCode ID: 67
Pattern: Strings / Simulation
Difficulty: Easy
Time Complexity: O(max(n, m))
Space Complexity: O(max(n, m))
Approach:
1. Traverse both binary strings from right to left.
2. Maintain carry during addition.
3. At each step:
- Add current bits + carry
- Append sum % 2 to answer
- Update carry = sum // 2
4. Reverse final result and return.
"""
class Solution:
def addBinary(self, a: str, b: str) -> str:
i = len(a) - 1
j = len(b) - 1
carry = 0
result = []
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))