-
Notifications
You must be signed in to change notification settings - Fork 5
/
MultiplyStrings.java
236 lines (200 loc) · 6.33 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*https://leetcode.com/problems/multiply-strings*/
/*Prateekshya*/
class Node
{
int data;
Node next;
Node(int data)
{
this.data = data;
}
}
class LL
{
public Node head, tail;
LL()
{
head = new Node(-1);
tail = head;
}
public void addNode(int data)
{
tail.next = new Node(data);
tail = tail.next;
}
public void finalizeList()
{
head = head.next;
}
}
class Solution
{
public String multiply(String num1, String num2)
{
//base cases
if (num1.equals("0") || num2.equals("0")) return "0";
if (num1.equals("1")) return num2;
if (num2.equals("1")) return num1;
//build two linked lists from the given strings
LL n1 = new LL();
for (int i = num1.length()-1; i >= 0; --i)
n1.addNode(Integer.parseInt(Character.toString(num1.charAt(i))));
n1.finalizeList();
LL n2 = new LL();
for (int i = num2.length()-1; i >= 0; --i)
n2.addNode(Integer.parseInt(Character.toString(num2.charAt(i))));
n2.finalizeList();
//build a result list
LL result = new LL();
Node n1Iter = n1.head, n2Iter = n2.head, rPoint = result.head, rIter = result.head;
int carry = 0;
//till the second list is exhausted
while (n2Iter != null)
{
//get the product and the carry
int prod = n1Iter.data*n2Iter.data;
prod += carry;
carry = prod/10;
prod %= 10;
//create a new node in result list with product value
rIter.next = new Node(prod);
rIter = rIter.next;
//move second list pointer
n2Iter = n2Iter.next;
}
//if still carry is there
while (carry > 0)
{
//keep adding new nodes till carry is exhausted
rIter.next = new Node(carry%10);
rIter = rIter.next;
carry /= 10;
}
//set the pointers
result.finalizeList();
rPoint = result.head.next;
rIter = result.head;
n1Iter = n1Iter.next;
n2Iter = n2.head;
//till the first list is exhausted
while (n1Iter != null)
{
carry = 0;
//till the second list is exhausted
while (n2Iter != null)
{
//get the product and the carry
int prod = n1Iter.data*n2Iter.data;
prod += carry;
//add product to the result list at its appropriate position
if (rIter.next == null)
{
carry = prod/10;
prod %= 10;
rIter.next = new Node(prod);
rIter = rIter.next;
if (rPoint == null) rPoint = rIter;
}
else
{
rIter = rIter.next;
rIter.data += prod;
carry = rIter.data/10;
rIter.data %= 10;
}
//move the second list pointer
n2Iter = n2Iter.next;
}
//if still carry is there
while (carry > 0)
{
//keep adding carry till exhausted
if (rIter.next == null)
{
rIter.next = new Node(carry%10);
rIter = rIter.next;
if (rPoint == null) rPoint = rIter;
}
else
{
rIter = rIter.next;
rIter.data += carry;
}
carry /= 10;
}
//set all the pointers
n2Iter = n2.head;
n1Iter = n1Iter.next;
rIter = rPoint;
rPoint = rPoint.next;
}
//convert the linkedlist to string
StringBuffer res = new StringBuffer("");
rIter = result.head;
while (rIter != null)
{
StringBuffer temp = new StringBuffer(Integer.toString(rIter.data));
temp.append(res);
res = temp;
rIter = rIter.next;
}
return new String(res);
}
}
/*Efficient solution*/
class Solution {
public String multiply(String num1, String num2) {
//base cases
if (num1.equals("0") || num2.equals("0")) return "0";
//create array
int[] temp = new int[num1.length() + num2.length() - 1];
//convert strings to character arrays
char[] chn1 = num1.toCharArray();
char[] chn2 = num2.toCharArray();
//for all digits of 1st number in reverse order
for (int i = chn1.length - 1; i >= 0; i--) {
//for all digits of 2nd number in reverse order
for (int j = chn2.length - 1; j >= 0; j--) {
//mulitply and add to the proper position
temp[i + j] += (chn1[i] - '0')*(chn2[j] - '0');
}
}
//set carry to 0
int carry = 0;
//for each position of the result in reverse order
for (int i = temp.length - 1; i >= 0; i--) {
//carry forward the carry part
carry += temp[i];
temp[i] = carry % 10;
carry /= 10;
}
//convert to string
StringBuilder sb = new StringBuilder();
if (carry > 0) sb.append(carry);
for (int t: temp) sb.append(t);
return sb.toString();
}
}
class Solution
{
public String multiply(String num1, String num2)
{
if(num1.equals("0") || num2.equals("0")) return "0";
char[] chNum1 = num1.toCharArray();
char[] chNum2 = num2.toCharArray();
int[] ans = new int[chNum1.length + chNum2.length - 1];
for(int i = 0; i < chNum1.length; i++)
for(int j = 0; j < chNum2.length; j++)
ans[i+j] += (chNum1[i] - '0') * (chNum2[j] - '0');
for(int i = ans.length - 1; i >= 1; i--)
{
ans[i-1] += ans[i] / 10;
ans[i] %= 10;
}
StringBuilder sb = new StringBuilder();
for(int i : ans) {
sb.append(i);
}
return sb.toString();
}
}