FunctionRacerProblemsRace

Subtract Digit Strings

EasyStringsPython, JavaScript, TypeScript, Java, C++

a and b are non-negative whole numbers written as digit strings with no leading zeros (zero itself is written "0"), and a is greater than or equal to b. Return a - b as a digit string with no leading zeros, and "0" when the two numbers are equal.

Work digit by digit from the right with a borrow, as on paper: the numbers can have up to 40 digits, far more than a 64-bit integer holds.

Example 1

a "1000"
b "1"
returns "999"

Subtracting 1 from 1000 borrows through every zero.

Example 2

a "5"
b "5"
returns "0"

The numbers are equal, so the answer is zero written as a single digit.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems