@@ -7,7 +7,7 @@ reference: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
7
7
*/
8
8
9
9
const defaultMatch = function ( array1 , array2 , index1 , index2 ) {
10
- return array1 [ index1 ] === array2 [ index2 ] ;
10
+ return array1 [ index1 ] === array2 [ index2 ] ;
11
11
} ;
12
12
13
13
const lengthMatrix = function ( array1 , array2 , match , context ) {
@@ -16,56 +16,56 @@ const lengthMatrix = function(array1, array2, match, context) {
16
16
let x , y ;
17
17
18
18
// initialize empty matrix of len1+1 x len2+1
19
- let matrix = [ len1 + 1 ] ;
19
+ let matrix = [ len1 + 1 ] ;
20
20
for ( x = 0 ; x < len1 + 1 ; x ++ ) {
21
- matrix [ x ] = [ len2 + 1 ] ;
21
+ matrix [ x ] = [ len2 + 1 ] ;
22
22
for ( y = 0 ; y < len2 + 1 ; y ++ ) {
23
- matrix [ x ] [ y ] = 0 ;
23
+ matrix [ x ] [ y ] = 0 ;
24
24
}
25
25
}
26
26
matrix . match = match ;
27
27
// save sequence lengths for each coordinate
28
28
for ( x = 1 ; x < len1 + 1 ; x ++ ) {
29
29
for ( y = 1 ; y < len2 + 1 ; y ++ ) {
30
30
if ( match ( array1 , array2 , x - 1 , y - 1 , context ) ) {
31
- matrix [ x ] [ y ] = matrix [ x - 1 ] [ y - 1 ] + 1 ;
31
+ matrix [ x ] [ y ] = matrix [ x - 1 ] [ y - 1 ] + 1 ;
32
32
} else {
33
- matrix [ x ] [ y ] = Math . max ( matrix [ x - 1 ] [ y ] , matrix [ x ] [ y - 1 ] ) ;
33
+ matrix [ x ] [ y ] = Math . max ( matrix [ x - 1 ] [ y ] , matrix [ x ] [ y - 1 ] ) ;
34
34
}
35
35
}
36
36
}
37
37
return matrix ;
38
38
} ;
39
39
40
- const backtrack = function ( matrix , array1 , array2 , index1 , index2 , context ) {
41
- if ( index1 === 0 || index2 === 0 ) {
42
- return {
43
- sequence : [ ] ,
44
- indices1 : [ ] ,
45
- indices2 : [ ] ,
46
- } ;
47
- }
48
-
49
- if ( matrix . match ( array1 , array2 , index1 - 1 , index2 - 1 , context ) ) {
50
- const subsequence = backtrack (
51
- matrix ,
52
- array1 ,
53
- array2 ,
54
- index1 - 1 ,
55
- index2 - 1 ,
56
- context
57
- ) ;
58
- subsequence . sequence . push ( array1 [ index1 - 1 ] ) ;
59
- subsequence . indices1 . push ( index1 - 1 ) ;
60
- subsequence . indices2 . push ( index2 - 1 ) ;
61
- return subsequence ;
62
- }
40
+ const backtrack = function ( matrix , array1 , array2 , context ) {
41
+ let index1 = array1 . length ;
42
+ let index2 = array2 . length ;
43
+ const subsequence = {
44
+ sequence : [ ] ,
45
+ indices1 : [ ] ,
46
+ indices2 : [ ] ,
47
+ } ;
63
48
64
- if ( matrix [ index1 ] [ index2 - 1 ] > matrix [ index1 - 1 ] [ index2 ] ) {
65
- return backtrack ( matrix , array1 , array2 , index1 , index2 - 1 , context ) ;
66
- } else {
67
- return backtrack ( matrix , array1 , array2 , index1 - 1 , index2 , context ) ;
49
+ while ( index1 !== 0 && index2 !== 0 ) {
50
+ const sameLetter =
51
+ matrix . match ( array1 , array2 , index1 - 1 , index2 - 1 , context ) ;
52
+ if ( sameLetter ) {
53
+ subsequence . sequence . unshift ( array1 [ index1 - 1 ] ) ;
54
+ subsequence . indices1 . unshift ( index1 - 1 ) ;
55
+ subsequence . indices2 . unshift ( index2 - 1 ) ;
56
+ -- index1 ;
57
+ -- index2 ;
58
+ } else {
59
+ const valueAtMatrixAbove = matrix [ index1 ] [ index2 - 1 ] ;
60
+ const valueAtMatrixLeft = matrix [ index1 - 1 ] [ index2 ] ;
61
+ if ( valueAtMatrixAbove > valueAtMatrixLeft ) {
62
+ -- index2 ;
63
+ } else {
64
+ -- index1 ;
65
+ }
66
+ }
68
67
}
68
+ return subsequence ;
69
69
} ;
70
70
71
71
const get = function ( array1 , array2 , match , context ) {
@@ -80,8 +80,6 @@ const get = function(array1, array2, match, context) {
80
80
matrix ,
81
81
array1 ,
82
82
array2 ,
83
- array1 . length ,
84
- array2 . length ,
85
83
innerContext
86
84
) ;
87
85
if ( typeof array1 === 'string' && typeof array2 === 'string' ) {
0 commit comments