Add two very large numbers given as strings
Coding problemMediumAsked in: PhonePe
Given two non-negative integers a and b written as strings of decimal digits, return their sum as a string of digits.
The numbers can have up to 100,000 digits, far more than any built-in integer type holds, so add them digit by digit. Neither input has leading zeros, except the number 0 itself, and the result must not have leading zeros either.
Examples
Example 1
Input: a = "11", b = "123"
Output: "134"
Example 2
Input: a = "999", b = "1"
Output: "1000"
Example 3
Input: a = "0", b = "0"
Output: "0"
Constraints
- a and b each have between 1 and 100,000 digits
- They hold only the digits 0 to 9, with no leading zeros unless the number is 0
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.