304 North Cardinal St.
Dorchester Center, MA 02124

# Multiply Strings LeetCode Solution

## Problem – Multiply Strings LeetCode Solution

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.

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.

## Multiply Strings LeetCode Solution in Java

``````public String multiply(String num1, String num2) {
int m = num1.length(), n = num2.length();
int[] pos = new int[m + n];

for(int i = m - 1; i >= 0; i--) {
for(int j = n - 1; j >= 0; j--) {
int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
int p1 = i + j, p2 = i + j + 1;
int sum = mul + pos[p2];

pos[p1] += sum / 10;
pos[p2] = (sum) % 10;
}
}

StringBuilder sb = new StringBuilder();
for(int p : pos) if(!(sb.length() == 0 && p == 0)) sb.append(p);
return sb.length() == 0 ? "0" : sb.toString();
}
``````

## Multiply Strings LeetCode Solution in C++

``````string multiply(string num1, string num2) {
string sum(num1.size() + num2.size(), '0');

for (int i = num1.size() - 1; 0 <= i; --i) {
int carry = 0;
for (int j = num2.size() - 1; 0 <= j; --j) {
int tmp = (sum[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0') + carry;
sum[i + j + 1] = tmp % 10 + '0';
carry = tmp / 10;
}
sum[i] += carry;
}

size_t startpos = sum.find_first_not_of("0");
if (string::npos != startpos) {
return sum.substr(startpos);
}
return "0";
}
``````

## Multiply Strings LeetCode Solution in Python

``````def multiply(num1, num2):
product = [0] * (len(num1) + len(num2))
pos = len(product)-1

for n1 in reversed(num1):
tempPos = pos
for n2 in reversed(num2):
product[tempPos] += int(n1) * int(n2)
product[tempPos-1] += product[tempPos]/10
product[tempPos] %= 10
tempPos -= 1
pos -= 1

pt = 0
while pt < len(product)-1 and product[pt] == 0:
pt += 1

return ''.join(map(str, product[pt:]))
``````
##### Multiply Strings LeetCode Solution Review:

In our experience, we suggest you solve this Multiply Strings LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Multiply Strings LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Multiply Strings LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions