1
1
/**
2
+ * Matrix
3
+ * Time O(N * M) | Space O(N + M)
4
+ * https://leetcode.com/problems/multiply-strings/
2
5
* @param {string } num1
3
6
* @param {string } num2
4
7
* @return {string }
5
8
*/
6
- var multiply = function ( num1 , num2 ) {
7
- if ( num1 === '0' || num2 === '0' ) {
8
- return '0' ;
9
- }
9
+ var multiply = ( num1 , num2 ) => {
10
+ const isZero = ( ( num1 === '0' ) || ( num2 === '0' ) ) ;
11
+ if ( isZero ) return '0' ;
12
+
13
+ const buffer = initBuffer ( num1 , num2 ) ; /* | Space (N + M) */
14
+
15
+ multiplication ( num1 , num2 , buffer ) /* Time O(N * M) */
16
+ removeLeadingZero ( buffer ) ; /* Time O(N + M) | Time O(N + M)*/
17
+
18
+ return buffer . join ( '' ) ; /* Time O(N + M) | Space O(N + M) */
19
+ } ;
10
20
11
- var res = new Array ( num1 . length + num2 . length ) . fill ( 0 ) ;
12
- var revNum1 = [ ...num1 ] . reverse ( ) ;
13
- var revNum2 = [ ...num2 ] . reverse ( ) ;
14
-
15
- for ( var i1 in revNum1 ) {
16
- for ( var i2 in revNum2 ) {
17
- var digit = revNum1 [ i1 ] * revNum2 [ i2 ] ;
18
- var index = Number ( i1 ) + Number ( i2 ) ;
19
- res [ index ] += digit ;
20
- res [ index + 1 ] += Math . floor ( res [ index ] / 10 ) ;
21
- res [ index ] = res [ index ] % 10 ;
21
+ var initBuffer = ( num1 , num2 ) => {
22
+ const size = ( num1 . length + num2 . length ) ;
23
+
24
+ return new Array ( size ) . fill ( 0 ) ; /* Space (N + M) */
25
+ }
26
+
27
+ var multiplication = ( num1 , num2 , buffer ) => {
28
+ for ( let i = ( num1 . length - 1 ) ; ( 0 <= i ) ; i -- ) { /* Time O(N) */
29
+ for ( let j = ( num2 . length - 1 ) ; ( 0 <= j ) ; j -- ) { /* Time O(M) */
30
+ update ( num1 , i , num2 , j , buffer ) ; /* Space O(N + M) */
22
31
}
23
32
}
33
+ }
24
34
25
- if ( res [ res . length - 1 ] === 0 ) {
26
- res . pop ( ) ;
27
- }
35
+ var removeLeadingZero = ( buffer ) => {
36
+ const isLeadZero = ( buffer [ 0 ] === 0 ) ;
37
+ if ( ! isLeadZero ) return ;
38
+
39
+ buffer . shift ( ) ; /* Time O(N + M) | Time O(N + M) */
40
+ }
41
+
42
+ var update = ( num1 , i , num2 , j , buffer ) => {
43
+ const curPos = ( i + j ) ;
44
+ const prevPos = curPos + 1 ;
28
45
29
- return res . reverse ( ) . join ( '' ) ;
46
+ const carry = buffer [ prevPos ] ;
47
+ const product = getProduct ( num1 , i , num2 , j ) ;
48
+ const sum = ( carry + product ) ;
49
+
50
+ const remainder = ( sum % 10 ) ;
51
+ const value = ( ( sum - remainder ) / 10 ) ;
52
+
53
+ buffer [ prevPos ] = remainder ; /* Space O(N + M) */
54
+ buffer [ curPos ] += value ; /* Space O(N + M) */
55
+ }
56
+
57
+ var getProduct = ( num1 , i , num2 , j ) => {
58
+ const [ iNum , jNum ] = [ Number ( num1 [ i ] ) , Number ( num2 [ j ] ) ] ;
59
+
60
+ return ( iNum * jNum ) ;
61
+ }
62
+
63
+ /**
64
+ * Matrix
65
+ * Time O(N * M) | Space O(N + M)
66
+ * https://leetcode.com/problems/multiply-strings/
67
+ * @param {string } num1
68
+ * @param {string } num2
69
+ * @return {string }
70
+ */
71
+ var multiply = ( num1 , num2 ) => {
72
+ const isZero = ( ( num1 === '0' ) || ( num2 === '0' ) ) ;
73
+ if ( isZero ) return '0' ;
74
+
75
+ const buffer = initBuffer ( num1 , num2 ) ; /* | Space O(N + M) */
76
+
77
+ multiplication ( num1 , num2 , buffer ) ; /* Time O(N * M) | Space O(N + M) */
78
+ removeLeadingZero ( buffer ) ; /* Time O(N + M) | Space O(N + M) */
79
+
80
+ return buffer . join ( '' ) ; /* Time O(N + M) | Space O(N + M) */
30
81
} ;
82
+
83
+ var initBuffer = ( num1 , num2 ) => new Array ( num1 . length + num2 . length ) . fill ( 0 ) ; /* Space O(N + M) */
84
+
85
+ var multiplication = ( num1 , num2 , buffer ) => {
86
+ [ num1 , num2 ] = /* Time O(N + M) */
87
+ [ reverse ( num1 ) , reverse ( num2 ) ] ;
88
+
89
+ for ( var i1 in num1 ) { /* Time O(N) */
90
+ for ( var i2 in num2 ) { /* Time O(M) */
91
+ update ( num1 , i1 , num2 , i2 , buffer ) ; /* Space O(N + M) */
92
+ }
93
+ }
94
+
95
+ buffer . reverse ( ) ; /* Time O(N + M) */
96
+ }
97
+
98
+ const reverse = ( s ) => s
99
+ . split ( '' ) /* Time O(K) | Space O (K) */
100
+ . reverse ( ) ; /* Time O(K) */
101
+
102
+ var update = ( num1 , i1 , num2 , i2 , buffer ) => {
103
+ const product = num1 [ i1 ] * num2 [ i2 ] ;
104
+ const index = Number ( i1 ) + Number ( i2 ) ;
105
+
106
+ buffer [ index ] += product ; /* Space O(N + M) */
107
+ buffer [ ( index + 1 ) ] += Math . floor ( buffer [ index ] / 10 ) ; /* Space O(N + M) */
108
+ buffer [ index ] = ( buffer [ index ] % 10 ) ; /* Space O(N + M) */
109
+ }
110
+
111
+ var removeLeadingZero = ( buffer ) => {
112
+ const isZero = ( buffer [ 0 ] === 0 ) ;
113
+ if ( ! isZero ) return ;
114
+
115
+ buffer . shift ( ) ; /* Time O(N + M) | Space O(N + M) */
116
+ }
0 commit comments