|
| 1 | +class Solution: |
| 2 | + """Base class for all LeetCode Problems.""" |
| 3 | + |
| 4 | + def addStrings(self, num1: str, num2: str) -> str: |
| 5 | + """ |
| 6 | + Given two non-negative integers, num1 and num2 represented as string, return the |
| 7 | + sum of num1 and num2 as a string. |
| 8 | +
|
| 9 | + You must solve the problem without using any built-in library for handling large |
| 10 | + integers (such as BigInteger). You must also not convert the inputs to integers |
| 11 | + directly. |
| 12 | + """ |
| 13 | + # Time Complexity: O(n) |
| 14 | + # Space Complexity: O(n) |
| 15 | + |
| 16 | + # Addition Rules (num1, num2) => (res, rem) |
| 17 | + RULES = { |
| 18 | + ("0", "0"): ("0", "0"), |
| 19 | + ("0", "1"): ("1", "0"), |
| 20 | + ("0", "2"): ("2", "0"), |
| 21 | + ("0", "3"): ("3", "0"), |
| 22 | + ("0", "4"): ("4", "0"), |
| 23 | + ("0", "5"): ("5", "0"), |
| 24 | + ("0", "6"): ("6", "0"), |
| 25 | + ("0", "7"): ("7", "0"), |
| 26 | + ("0", "8"): ("8", "0"), |
| 27 | + ("0", "9"): ("9", "0"), |
| 28 | + ("1", "0"): ("1", "0"), |
| 29 | + ("1", "1"): ("2", "0"), |
| 30 | + ("1", "2"): ("3", "0"), |
| 31 | + ("1", "3"): ("4", "0"), |
| 32 | + ("1", "4"): ("5", "0"), |
| 33 | + ("1", "5"): ("6", "0"), |
| 34 | + ("1", "6"): ("7", "0"), |
| 35 | + ("1", "7"): ("8", "0"), |
| 36 | + ("1", "8"): ("9", "0"), |
| 37 | + ("1", "9"): ("0", "1"), |
| 38 | + ("2", "0"): ("2", "0"), |
| 39 | + ("2", "1"): ("3", "0"), |
| 40 | + ("2", "2"): ("4", "0"), |
| 41 | + ("2", "3"): ("5", "0"), |
| 42 | + ("2", "4"): ("6", "0"), |
| 43 | + ("2", "5"): ("7", "0"), |
| 44 | + ("2", "6"): ("8", "0"), |
| 45 | + ("2", "7"): ("9", "0"), |
| 46 | + ("2", "8"): ("0", "1"), |
| 47 | + ("2", "9"): ("1", "1"), |
| 48 | + ("3", "0"): ("3", "0"), |
| 49 | + ("3", "1"): ("4", "0"), |
| 50 | + ("3", "2"): ("5", "0"), |
| 51 | + ("3", "3"): ("6", "0"), |
| 52 | + ("3", "4"): ("7", "0"), |
| 53 | + ("3", "5"): ("8", "0"), |
| 54 | + ("3", "6"): ("9", "0"), |
| 55 | + ("3", "7"): ("0", "1"), |
| 56 | + ("3", "8"): ("1", "1"), |
| 57 | + ("3", "9"): ("2", "1"), |
| 58 | + ("4", "0"): ("4", "0"), |
| 59 | + ("4", "1"): ("5", "0"), |
| 60 | + ("4", "2"): ("6", "0"), |
| 61 | + ("4", "3"): ("7", "0"), |
| 62 | + ("4", "4"): ("8", "0"), |
| 63 | + ("4", "5"): ("9", "0"), |
| 64 | + ("4", "6"): ("0", "1"), |
| 65 | + ("4", "7"): ("1", "1"), |
| 66 | + ("4", "8"): ("2", "1"), |
| 67 | + ("4", "9"): ("3", "1"), |
| 68 | + ("5", "0"): ("5", "0"), |
| 69 | + ("5", "1"): ("6", "0"), |
| 70 | + ("5", "2"): ("7", "0"), |
| 71 | + ("5", "3"): ("8", "0"), |
| 72 | + ("5", "4"): ("9", "0"), |
| 73 | + ("5", "5"): ("0", "1"), |
| 74 | + ("5", "6"): ("1", "1"), |
| 75 | + ("5", "7"): ("2", "1"), |
| 76 | + ("5", "8"): ("3", "1"), |
| 77 | + ("5", "9"): ("4", "1"), |
| 78 | + ("6", "0"): ("6", "0"), |
| 79 | + ("6", "1"): ("7", "0"), |
| 80 | + ("6", "2"): ("8", "0"), |
| 81 | + ("6", "3"): ("9", "0"), |
| 82 | + ("6", "4"): ("0", "1"), |
| 83 | + ("6", "5"): ("1", "1"), |
| 84 | + ("6", "6"): ("2", "1"), |
| 85 | + ("6", "7"): ("3", "1"), |
| 86 | + ("6", "8"): ("4", "1"), |
| 87 | + ("6", "9"): ("5", "1"), |
| 88 | + ("7", "0"): ("7", "0"), |
| 89 | + ("7", "1"): ("8", "0"), |
| 90 | + ("7", "2"): ("9", "0"), |
| 91 | + ("7", "3"): ("0", "1"), |
| 92 | + ("7", "4"): ("1", "1"), |
| 93 | + ("7", "5"): ("2", "1"), |
| 94 | + ("7", "6"): ("3", "1"), |
| 95 | + ("7", "7"): ("4", "1"), |
| 96 | + ("7", "8"): ("5", "1"), |
| 97 | + ("7", "9"): ("6", "1"), |
| 98 | + ("8", "0"): ("8", "0"), |
| 99 | + ("8", "1"): ("9", "0"), |
| 100 | + ("8", "2"): ("0", "1"), |
| 101 | + ("8", "3"): ("1", "1"), |
| 102 | + ("8", "4"): ("2", "1"), |
| 103 | + ("8", "5"): ("3", "1"), |
| 104 | + ("8", "6"): ("4", "1"), |
| 105 | + ("8", "7"): ("5", "1"), |
| 106 | + ("8", "8"): ("6", "1"), |
| 107 | + ("8", "9"): ("7", "1"), |
| 108 | + ("9", "0"): ("9", "0"), |
| 109 | + ("9", "1"): ("0", "1"), |
| 110 | + ("9", "2"): ("1", "1"), |
| 111 | + ("9", "3"): ("2", "1"), |
| 112 | + ("9", "4"): ("3", "1"), |
| 113 | + ("9", "5"): ("4", "1"), |
| 114 | + ("9", "6"): ("5", "1"), |
| 115 | + ("9", "7"): ("6", "1"), |
| 116 | + ("9", "8"): ("7", "1"), |
| 117 | + ("9", "9"): ("8", "1"), |
| 118 | + } |
| 119 | + |
| 120 | + # Get the smallest and largest number |
| 121 | + if len(num1) <= len(num2): |
| 122 | + smallest = num1 |
| 123 | + largest = num2 |
| 124 | + else: |
| 125 | + smallest = num2 |
| 126 | + largest = num1 |
| 127 | + |
| 128 | + # Add leading zeros to the smallest number |
| 129 | + smallest = "0" * (len(largest) - len(smallest)) + smallest |
| 130 | + |
| 131 | + # Loop through the digits from right to left |
| 132 | + result = "" |
| 133 | + remaining = "0" |
| 134 | + for n1, n2 in zip(reversed(smallest), reversed(largest)): |
| 135 | + # Apply the addition rules including remaining |
| 136 | + add, next_remaining1 = RULES[(n1, n2)] |
| 137 | + add, next_remaining2 = RULES[(add, remaining)] |
| 138 | + |
| 139 | + # Determine the next remaining value |
| 140 | + if next_remaining1 == "1": |
| 141 | + next_remaining = next_remaining1 |
| 142 | + else: |
| 143 | + next_remaining = next_remaining2 |
| 144 | + |
| 145 | + # Update the result and remaining |
| 146 | + result = add + result |
| 147 | + remaining = next_remaining |
| 148 | + |
| 149 | + # Add the remaining value if it's not zero |
| 150 | + if remaining == "1": |
| 151 | + result = remaining + result |
| 152 | + |
| 153 | + return result |
0 commit comments