↓Skip to main content
  1. LeetCode/

LeetCode 415: Add Strings

·2 mins· ·
LeetCode Easy Math String Simulation
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-24

  • Submissions: Runtime Error ×4 → Wrong Answer ×2 → Accepted

Approach
#

Column addition (Math Template 6): i and j walk from the ends of the two strings, treating a finished one as 0. Each step computes total = a + b + carry, writes total % 10 and carries total // 10. If a carry remains after the loop, append one more digit. Reverse at the end.

My Attempts
#

This took 7 submissions:

  • Attempt 1 was a syntax error, then i = j = len(num1) - 1, len(num2) - 1 made both variables the same tuple (TypeError); the next two forgot to convert characters to int.
  • After that the final carry was dropped: "8" + "6" returned "4". The next version appended total instead of the carry temp, returning "44". Appending temp passed.

Solution
#

class Solution:
    def addStrings(self, num1: str, num2: str) -> str:
        i, j, carry = len(num1) - 1, len(num2) - 1, 0
        res = []
        while i >= 0 or j >= 0 or carry:
            a = int(num1[i]) if i >= 0 else 0
            b = int(num2[j]) if j >= 0 else 0
            carry, d = divmod(a + b + carry, 10)
            res.append(str(d))
            i, j = i - 1, j - 1
        return "".join(reversed(res))

Complexity
#

Time O(max(n, m)), space O(max(n, m)).

Mistakes to Avoid
#

  • Append the final carry: "8" + "6" is "14".
  • Append the carry, not the last total.
  • i = j = a, b makes both variables tuples.

Interview Explanation
#

Two pointers add digits from the ends, treating a finished string as 0 and tracking the carry; putting the carry in the loop condition keeps the final one, and I reverse the result. Time O(max(n, m)).

Related

LeetCode 412: Fizz Buzz
·1 min
LeetCode Easy Math String Simulation
LeetCode 412 note: check multiples of 15 first, then 5 and 3; multiples need %, not //
LeetCode 67: Add Binary
·2 mins
LeetCode Easy Math String Bit-Manipulation Simulation
LeetCode 67 note: add from the ends of both strings digit by digit, with carry sum // 2 and digit sum % 2
LeetCode 405: Convert a Number to Hexadecimal
·2 mins
LeetCode Easy Math String Bit-Manipulation
LeetCode 405 note: add 2^32 to a negative number to get its two’s complement, then take 4 bits at a time (% 16); handle 0 separately
LeetCode 258: Add Digits
·1 min
LeetCode Easy Math Simulation Number-Theory
LeetCode 258 note: sum the digits repeatedly until one remains; the O(1) answer is the digital root 1 + (n - 1) % 9
LeetCode 171: Excel Sheet Column Number
·1 min
LeetCode Easy Math String
LeetCode 171 note: scan left to right with res = res × 26 + letter value (A = 1)
LeetCode 168: Excel Sheet Column Title
·2 mins
LeetCode Easy Math String
LeetCode 168 note: Excel columns are base 26 without a zero digit; take (n - 1) % 26 as the letter, then n = (n - 1) // 26