8
8
/**
9
9
* * Longest Common Subsequence (LCS) Problem
10
10
*/
11
-
12
11
public class Solution {
13
12
public int longestCommonSubsequence (String s1 , String s2 ) {
14
13
// return longestCommonSubsequenceRec(
@@ -19,7 +18,8 @@ public int longestCommonSubsequence(String s1, String s2) {
19
18
// int[][] cache = new int[s1.length() + 1][s2.length() + 1];
20
19
// for (int[] row : cache) Arrays.fill(row, -1);
21
20
22
- // return longestCommonSubsequenceMem(s1.length(), s2.length(), s1.toCharArray(), s2.toCharArray(), cache);
21
+ // return longestCommonSubsequenceMem(s1.length(), s2.length(), s1.toCharArray(),
22
+ // s2.toCharArray(), cache);
23
23
24
24
return longestCommonSubsequenceDP (s1 , s2 );
25
25
@@ -28,7 +28,7 @@ public int longestCommonSubsequence(String s1, String s2) {
28
28
29
29
/**
30
30
* * Dynamic Programming Approach (Space Optimized)
31
- *
31
+ *
32
32
* * TC: O(mn)
33
33
* * SC: O(n)
34
34
*/
@@ -38,7 +38,7 @@ public int longestCommonSubsequence(String s1, String s2) {
38
38
39
39
// for (int i = 1; i < m + 1; i++) {
40
40
// // Calculate whether the the
41
- // // current row is even/odd.
41
+ // // current row is even/odd.
42
42
// idx = i & 1;
43
43
// for (int j = 1; j < n + 1; j++) {
44
44
// if (s1.charAt(i - 1) == s2.charAt(j - 1))
@@ -56,7 +56,7 @@ public int longestCommonSubsequence(String s1, String s2) {
56
56
57
57
/**
58
58
* * Dynamic Programming Approach
59
- *
59
+ *
60
60
* * TC: O(mn)
61
61
* * SC: O(mn)
62
62
*/
@@ -66,13 +66,8 @@ private int longestCommonSubsequenceDP(String s1, String s2) {
66
66
67
67
for (int i = 1 ; i < m + 1 ; i ++) {
68
68
for (int j = 1 ; j < n + 1 ; j ++) {
69
- if (s1 .charAt (i - 1 ) == s2 .charAt (j - 1 ))
70
- dp [i ][j ] = 1 + dp [i - 1 ][j - 1 ];
71
- else
72
- dp [i ][j ] = Math .max (
73
- dp [i - 1 ][j ],
74
- dp [i ][j - 1 ]
75
- );
69
+ if (s1 .charAt (i - 1 ) == s2 .charAt (j - 1 )) dp [i ][j ] = 1 + dp [i - 1 ][j - 1 ];
70
+ else dp [i ][j ] = Math .max (dp [i - 1 ][j ], dp [i ][j - 1 ]);
76
71
}
77
72
}
78
73
@@ -91,8 +86,7 @@ private String buildSequence(String s1, String s2, int[][] dp) {
91
86
seq .add (0 , String .valueOf (s1 .charAt (i - 1 )));
92
87
--i ;
93
88
--j ;
94
- }
95
- else if (dp [i - 1 ][j ] > dp [i ][j - 1 ]) --i ;
89
+ } else if (dp [i - 1 ][j ] > dp [i ][j - 1 ]) --i ;
96
90
else --j ;
97
91
}
98
92
@@ -101,7 +95,7 @@ private String buildSequence(String s1, String s2, int[][] dp) {
101
95
102
96
/**
103
97
* * Memoization Approach
104
- *
98
+ *
105
99
* * TC: O(mn)
106
100
* * SC: O(mn)
107
101
*/
@@ -121,7 +115,7 @@ private String buildSequence(String s1, String s2, int[][] dp) {
121
115
122
116
/**
123
117
* * Recursive Approach
124
- *
118
+ *
125
119
* * TC: O(3^(m + n)) approximately
126
120
* * SC: O(3^(m + n)) approximately
127
121
*/
@@ -146,4 +140,4 @@ public static void main(String[] args) {
146
140
// should be 4
147
141
System .out .println (solution .longestCommonSubsequence ("AGGTAB" , "GXTXAYB" ));
148
142
}
149
- }
143
+ }
0 commit comments