Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-273

Big Integer Subtraction

Problem

Subtract one non-negative integer string from another and return the difference as a string, without converting the inputs to integers.

Walk both strings from the right with a borrow, then strip leading zeros. Assume the first number is greater than or equal to the second.

Hint

Keep the result digits in reverse while computing, then flip them at the end.

Test cases

case 1
in: ["100","1"]
expected: "99"
case 2
in: ["12345","12345"]
expected: "0"
case 3
in: ["1000","999"]
expected: "1"
case 4
in: ["5","0"]
expected: "5"
case 5
in: ["1000000000000000000000","1"]
expected: "999999999999999999999"
Starter codePython
def big_subtract(a, b):
    # Your code here
    pass

5 test cases · run your code against them in the editor