Multiply Strings

Posted by Bill on October 19, 2023

Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"


Constraints:

1 <= num1.length, num2.length <= 200
num1 and num2 consist of digits only.
Both num1 and num2 do not contain any leading zero, except the number 0 itself.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
  public:
  string multiply(string num1, string num2) {
    int len1 = num1.size();
    int len2 = num2.size();
    string ans;
    for (int i = 0; i < len1 + 1 + len2; i++) {
        ans.push_back('0');
    }
    for (int j = len2 - 1; j >= 0; j--) {
      int carry = 0;
      for (int i = len1 - 1; i >= 0; i--) {
        int tmp = (num1[i] - '0') * (num2[j] - '0');
        int round = len1 - 1 - i + len2 - 1 - j;
        int val = tmp + carry;
        val += (ans[round] - '0');
        ans[len2 - 1 - j + len1 - 1 -i] = val % 10 + '0';
        carry = val >= 10 ? val / 10 : 0;
      }
      if (carry != 0) {
        ans[len1 + len2 - 1 - j] = carry + '0';
      }
    }
    reverse(ans.begin(), ans.end());
    int nextZeroIndex;
    for (nextZeroIndex = 0; nextZeroIndex < ans.size() - 1; nextZeroIndex++) {
      if (ans[nextZeroIndex] != '0') {
        break;
      }
    }
    return ans.substr(nextZeroIndex);
  }
};