-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMultiply Strings.js
53 lines (42 loc) · 1.47 KB
/
Multiply Strings.js
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
// Runtime: 194 ms (Top 12.86%) | Memory: 49.9 MB (Top 18.17%)
var multiply = function(num1, num2) {
const m = num1.length;
const n = num2.length;
const steps = [];
let carry = 0;
for(let i = m - 1; i >= 0; i -= 1) {
const digitOne = parseInt(num1[i]);
let step = "0".repeat(m - 1 - i);
carry = 0;
for(let j = n - 1; j >= 0; j -= 1) {
const digitTwo = parseInt(num2[j]);
const product = digitOne * digitTwo + carry;
const newDigit = product % 10;
carry = Math.floor(product / 10);
step = newDigit + step;
}
if(carry > 0) step = carry + step;
steps.push(step);
}
for(let i = 0; i < steps.length - 1; i += 1) {
let nextStep = steps[i + 1];
let step = steps[i];
step = "0".repeat(nextStep.length - step.length) + step;
carry = 0;
let newStep = "";
for(let j = step.length - 1; j >= 0; j -= 1) {
const sum = parseInt(nextStep[j]) + parseInt(step[j]) + carry;
const digit = sum % 10;
carry = Math.floor(sum / 10);
newStep = digit + newStep;
}
if(carry > 0) newStep = carry + newStep;
steps[i + 1] = newStep;
}
let result = steps[steps.length - 1];
let leadingZeros = 0
while(leadingZeros < result.length - 1 && result[leadingZeros] === '0') {
leadingZeros += 1;
}
return result.slice(leadingZeros);
};