-
Notifications
You must be signed in to change notification settings - Fork 820
/
MultiplyStrings.java
74 lines (70 loc) · 2.28 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* (C) 2024 YourCompanyName */
package string;
/**
* Created by gouthamvidyapradhan on 01/02/2018. Given two non-negative integers num1 and num2
* represented as strings, return the product of num1 and num2.
*
* <p>Note:
*
* <p>The length of both num1 and num2 is < 110. Both num1 and num2 contains only digits 0-9. Both
* num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library
* or convert the inputs to integer directly.
*
* <p>Solution: O(N x M) where N and M are length of strings
*/
public class MultiplyStrings {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(new MultiplyStrings().multiply("00", "0000"));
}
public String multiply(String num1, String num2) {
if ((num1.length() == 1 && num1.equals("0")) || (num2.length() == 1 && num2.equals("0")))
return "0";
if (num1.length() < num2.length()) return multiply(num2, num1);
String temp2 = "", trail = "";
int carry = 0;
for (int i = 0; i < num1.length(); i++) {
temp2 += "0";
}
for (int i = num1.length() - 1; i >= 0; i--) {
String temp1 = "";
for (int j = num2.length() - 1; j >= 0; j--) {
int prod =
Integer.parseInt(String.valueOf(num2.charAt(j)))
* Integer.parseInt(String.valueOf(num1.charAt(i)));
prod += carry;
temp1 = (prod % 10) + temp1;
carry = (prod / 10);
}
if (carry > 0) {
temp1 = String.valueOf(carry) + temp1;
carry = 0;
}
String temp3 = add(temp1, temp2);
temp2 = temp3.substring(0, temp3.length() - 1);
trail = temp3.substring(temp3.length() - 1, temp3.length()) + trail;
}
return temp2 + trail;
}
private String add(String s1, String s2) {
String result = "";
int carry = 0;
int i = s1.length() - 1, j = s2.length() - 1;
for (; i >= 0 || j >= 0; i--, j--) {
int l = (i >= 0) ? Integer.parseInt(String.valueOf(s1.charAt(i))) : 0;
int r = (j >= 0) ? Integer.parseInt(String.valueOf(s2.charAt(j))) : 0;
int sum = l + r + carry;
carry = sum / 10;
result = sum % 10 + result;
}
if (carry > 0) {
result = carry + result;
}
return result;
}
}