-
Notifications
You must be signed in to change notification settings - Fork 43
/
MultiplyStrings.java
255 lines (213 loc) · 8.29 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package LeetCodeJava.String;
// https://leetcode.com/problems/multiply-strings/description/
import java.util.ArrayList;
/**
* Similar LC problems
*
* 66. Plus One
* 67. Add Binary
* 415. Add Strings
* 989. Add to Array-Form of Integer
*
*/
public class MultiplyStrings {
// V0
// IDEA : STRING OP
// TODO : fix
// public String multiply(String num1, String num2) {
//
// if (num1.equals("0") || num2.equals("0")){
// return "0";
// }
//
// if (num1.equals("1") || num2.equals("1")){
// if (num1.equals("1")){
// return num2;
// }
// return num1;
// }
//
// int res = 0;
// Long num2Int = Long.parseLong(num2);
// Long num1Int = Long.parseLong(num1);
// while (num2Int > 0){
// res += num1Int;
// num2Int -= 1L;
// }
//
// return String.valueOf(res);
// }
// V1
// IDEA : BRUTE FORCE + STRING OP (gpt)
public String multiply_1(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
int m = num1.length();
int n = num2.length();
int[] result = new int[m + n];
/** NOTE !!!
*
* double loop
* i traverse from first string
* j traverse from second string
*/
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;
int p2 = i + j + 1;
int sum = mul + result[p2];
result[p1] += sum / 10;
result[p2] = sum % 10;
}
}
StringBuilder sb = new StringBuilder();
for (int num : result) {
if (!(sb.length() == 0 && num == 0)) {
sb.append(num);
}
}
return sb.length() == 0 ? "0" : sb.toString();
}
// V2
// https://leetcode.com/problems/multiply-strings/editorial/
// IDEA : MATH
// Calculate the sum of all of the results from multiplyOneDigit.
private StringBuilder sumResults(ArrayList<ArrayList<Integer>> results) {
// Initialize answer as a number from results.
ArrayList<Integer> answer = new ArrayList<>(
results.get(results.size() - 1)
);
ArrayList<Integer> newAnswer = new ArrayList<>();
// Sum each digit from answer and result
for (int j = 0; j < results.size() - 1; ++j) {
ArrayList<Integer> result = new ArrayList<>(results.get(j));
newAnswer = new ArrayList<>();
int carry = 0;
for (int i = 0; i < answer.size() || i < result.size(); ++i) {
// If answer is shorter than result or vice versa, use 0 as the current digit.
int digit1 = i < result.size() ? result.get(i) : 0;
int digit2 = i < answer.size() ? answer.get(i) : 0;
// Add current digits of both numbers.
int sum = digit1 + digit2 + carry;
// Set carry equal to the tens place digit of sum.
carry = sum / 10;
// Append the ones place digit of sum to answer.
newAnswer.add(sum % 10);
}
if (carry != 0) {
newAnswer.add(carry);
}
answer = newAnswer;
}
// Convert answer to a string.
StringBuilder finalAnswer = new StringBuilder();
for (int digit : answer) {
finalAnswer.append(digit);
}
return finalAnswer;
}
// Multiply the current digit of secondNumber with firstNumber.
ArrayList<Integer> multiplyOneDigit(
StringBuilder firstNumber,
char secondNumberDigit,
int numZeros
) {
// Insert zeros at the beginning based on the current digit's place.
ArrayList<Integer> currentResult = new ArrayList<>();
for (int i = 0; i < numZeros; ++i) {
currentResult.add(0);
}
int carry = 0;
// Multiply firstNumber with the current digit of secondNumber.
for (int i = 0; i < firstNumber.length(); ++i) {
char firstNumberDigit = firstNumber.charAt(i);
int multiplication =
(secondNumberDigit - '0') * (firstNumberDigit - '0') + carry;
// Set carry equal to the tens place digit of multiplication.
carry = multiplication / 10;
// Append last digit to the current result.
currentResult.add(multiplication % 10);
}
if (carry != 0) {
currentResult.add(carry);
}
return currentResult;
}
public String multiply_2_1(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
StringBuilder firstNumber = new StringBuilder(num1);
StringBuilder secondNumber = new StringBuilder(num2);
// Reverse both the numbers.
firstNumber.reverse();
secondNumber.reverse();
// For each digit in secondNumber, multipy the digit by firstNumber and
// store the multiplication result (reversed) in results.
ArrayList<ArrayList<Integer>> results = new ArrayList<>();
for (int i = 0; i < secondNumber.length(); ++i) {
results.add(
multiplyOneDigit(firstNumber, secondNumber.charAt(i), i)
);
}
// Add all the results in the results array, and store the sum in the answer string.
StringBuilder answer = sumResults(results);
// answer is reversed, so reverse it to get the final answer.
answer.reverse();
return answer.toString();
}
// V3
// https://leetcode.com/problems/multiply-strings/editorial/
// IDEA : MATH + less space usage
// V4
// https://leetcode.com/problems/multiply-strings/editorial/
// IDEA : Sum the products from all pairs of digits
public String multiply_4(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
StringBuilder firstNumber = new StringBuilder(num1);
StringBuilder secondNumber = new StringBuilder(num2);
// Reverse both the numbers.
firstNumber.reverse();
secondNumber.reverse();
// To store the multiplication result of each digit of secondNumber with firstNumber.
int N = firstNumber.length() + secondNumber.length();
StringBuilder answer = new StringBuilder();
for (int i = 0; i < N; ++i) {
answer.append(0);
}
for (int place2 = 0; place2 < secondNumber.length(); place2++) {
int digit2 = secondNumber.charAt(place2) - '0';
// For each digit in secondNumber multiply the digit by all digits in firstNumber.
for (int place1 = 0; place1 < firstNumber.length(); place1++) {
int digit1 = firstNumber.charAt(place1) - '0';
// The number of zeros from multiplying to digits depends on the
// place of digit2 in secondNumber and the place of the digit1 in firstNumber.
int currentPos = place1 + place2;
// The digit currently at position currentPos in the answer string
// is carried over and summed with the current result.
int carry = answer.charAt(currentPos) - '0';
int multiplication = digit1 * digit2 + carry;
// Set the ones place of the multiplication result.
answer.setCharAt(
currentPos,
(char) ((multiplication % 10) + '0')
);
// Carry the tens place of the multiplication result by
// adding it to the next position in the answer array.
int value =
(answer.charAt(currentPos + 1) - '0') + multiplication / 10;
answer.setCharAt(currentPos + 1, (char) (value + '0'));
}
}
// Pop excess 0 from the rear of answer.
if (answer.charAt(answer.length() - 1) == '0') {
answer.deleteCharAt(answer.length() - 1);
}
answer.reverse();
return answer.toString();
}
}