-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathMultiplyStrings.java
46 lines (37 loc) · 1008 Bytes
/
MultiplyStrings.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public String multiply(String num1, String num2) {
int m = num1.length();
int n = num2.length();
// Base Conditions
if (m == 0 || n == 0 || "0".equals(num1) || "0".equals(num2)) {
return "0";
}
if ("1".equals(num1)) {
return num2;
}
if ("1".equals(num2)) {
return num1;
}
// Result can be maximum of length M + N.
// For example 99 * 999 = 98901 (Result is of length 5)
int[] result = new int[m + n];
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
int product = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
// Adding previous values in result array into this product.
product += result[i + j + 1];
// Adding the new product into the result array
result[i + j + 1] = product % 10;
result[i + j] += product / 10;
}
}
StringBuilder sb = new StringBuilder();
for (int r : result) {
if (sb.length() == 0 && r == 0) {
continue;
}
sb.append(r);
}
return sb.toString();
}
}