-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0415-add-strings.py
More file actions
50 lines (40 loc) · 1.62 KB
/
0415-add-strings.py
File metadata and controls
50 lines (40 loc) · 1.62 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import List
import unittest
# https://leetcode.com/problems/add-strings/
# python3 -m unittest strings/0415-add-strings.py
class Solution(unittest.TestCase):
# # Approach #1: Brute Force - Convert to integers and add
# # Time: O(n + m) - string to int conversion
# # Space: O(1) - excluding output
# def addStringsBruteForce(self, num1: str, num2: str) -> str:
# return str(int(num1) + int(num2))
# Approach #2: Simulation with Two Pointers
# Time: O(max(n, m)) where n, m are lengths of num1, num2
# Space: O(max(n, m)) for result string
def addStrings(self, num1: str, num2: str) -> str:
i, j = len(num1) - 1, len(num2) - 1
carry = 0
result: List[str] = []
while i >= 0 or j >= 0 or carry:
# Get current digits (0 if index out of bounds)
digit1 = int(num1[i]) if i >= 0 else 0
digit2 = int(num2[j]) if j >= 0 else 0
# Calculate sum with carry
total = digit1 + digit2 + carry
carry = total // 10
result.append(str(total % 10))
i -= 1
j -= 1
# Result is built backwards, so reverse it
return "".join(reversed(result))
def test(self):
for num1, num2, expected in [
("11", "123", "134"),
("456", "77", "533"),
("0", "0", "0"),
("1", "9", "10"),
("999", "1", "1000"),
("123456789", "987654321", "1111111110"),
]:
output = self.addStrings(num1, num2)
self.assertEqual(expected, output, f"expected: {expected}, output: {output}")