Show problems as
Classic problem #415EasyMathStringSimulation

Add Strings

Real-world version: Exposure Counter Sum
Tracked in this browser only
Read Add Strings on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

An archival telescope keeps its shutter counters as decimal text, because the running totals outgrow every machine word the logger owns. You are handed two such counters, left and right, each a string of digits with no leading zeros unless the counter itself is "0".

Return the sum of the two counters, again as a digit string with no leading zeros.

The logger has no arbitrary-precision arithmetic available, so you must stay on the characters: do not convert left or right into a built-in big integer, do not call a library routine that parses a numeric string for you, and do not hand the work to a big-number package.

Examples

Example 1

Input
left = "907", right = "35"
Output
"942"

The two counters total 942, reported in the same digit-string form.

Example 2

Input
left = "6", right = "3994"
Output
"4000"

The total is 4000, which is as many digits as the longer counter.

Example 3

Input
left = "88", right = "12"
Output
"100"

The total is 100, one digit longer than either counter.

Example 4

Input
left = "40", right = "0"
Output
"40"

Adding the zero counter leaves the other counter unchanged.

Constraints

  • 1 <= left.length, right.length <= 10^4
  • left and right consist of digits only.
  • left and right have no leading zeros except for the single digit 0 itself.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def add_strings(left: str, right: str) -> str:
Java
public String addStrings(String left, String right)