Intermediate Level

Implement the multiplyLargeNumbersString method that multiplies two non-negative integers represented as strings.

You are given two non-negative integers as strings. Return their product as a string.

The numbers may be too large for normal integer types, so the multiplication must be performed manually using digit operations.

Example 1

Input
first (string) = 123
second (string) = 456
Returns (string) 56088

Example 2

Input
first (string) = 2
second (string) = 3
Returns (string) 6

Example 3

Input
first (string) = 0
second (string) = 999
Returns (string) 0

Simulate the grade-school multiplication method.

Create an integer array of length len(a) + len(b) to store digit results. Multiply each digit of the first number with each digit of the second number and add it to the correct position. After each addition, carry the extra value to the previous position.

Finally, skip leading zeros and convert the digit array into the result string.

Pseudocode:

function multiplyLargeNumbersString(first, second):
    if first == "0" or second == "0":
        return "0"
    result = array of length first.length + second.length filled with 0
    for i from first.length - 1 down to 0:
        for j from second.length - 1 down to 0:
            product = digit(first[i]) * digit(second[j])
            sum = product + result[i + j + 1]
            result[i + j + 1] = sum % 10
            result[i + j] += sum / 10
    return result digits as string without leading zeros
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.