63. Column Addition

A tiny calculator stores numbers as strings of digits. You are given two non-negative whole numbers a and b, written in decimal (digits 0 to 9).

Return their sum, also written as a string in decimal (digits 0 to 9). Neither input has leading zeros unless it is exactly "0", and the answer must follow the same rule. The numbers can be far longer than any built-in integer type, so add them digit by digit from the right while carrying.

Example 1

Input:
a = "758", b = "64"
Output:
"822"
Explanation:

758 + 64 = 822.

Example 2

Input:
a = "9", b = "991"
Output:
"1000"
Explanation:

9 + 991 = 1000; the carry runs through every column and adds a new leading digit.

Constraints

1 ≤ a.length, b.length ≤ 104

a and b contain only digits

No leading zeros except for the number 0 itself

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(max(n, m))
Space
O(max(n, m))

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…