4
4
5
5
DFS
6
6
7
- ``` java
8
- /**
9
- * Question : 1143. Longest Common Subsequence
10
- * Complexity : Time: O(2^n) ; Space: O(n)
11
- * Topics : DP, Backtracking
12
- */
13
- class Solution {
14
- int longest = 0 ;
15
-
16
- public int longestCommonSubsequence (String text1 , String text2 ) {
17
- longest = 0 ;
18
- longestCommonSubsequence(text1, 0 , text2, 0 , 0 );
19
- return longest;
20
- }
21
-
22
- private void longestCommonSubsequence (String text1 , int p1 , String text2 , int p2 , int count ) {
23
- if (p1 == text1. length() || p2 == text2. length()) {
24
- return ;
25
- }
26
- if (text1. charAt(p1) == text2. charAt(p2)) {
27
- longest = Math . max(longest, count + 1 );
28
- longestCommonSubsequence(text1, p1 + 1 , text2, p2 + 1 , count + 1 );
29
- } else {
30
- longestCommonSubsequence(text1, p1 + 1 , text2, p2, count);
31
- longestCommonSubsequence(text1, p1, text2, p2 + 1 , count);
32
- }
33
- }
34
- }
35
- ```
36
-
37
- ## Solution 2
38
-
39
- DFS
40
-
41
7
``` java
42
8
/**
43
9
* Question : 1143. Longest Common Subsequence
44
10
* Complexity : Time: O(2^n) ; Space: O(n)
45
11
* Topics : DP
46
12
*/
47
- public class Solution {
48
- public int longestCommonSubsequence (String text1 , String text2 ) {
49
- return longestCommonSubsequence(text1, 0 , text2, 0 );
50
- }
51
-
52
- private int longestCommonSubsequence (String text1 , int i , String text2 , int j ) {
53
- if (i >= text1. length() || j >= text2. length()) {
54
- return 0 ;
55
- }
56
-
57
- if (text1. charAt(i) == text2. charAt(j)) {
58
- return 1 + longestCommonSubsequence(text1, i + 1 , text2, j + 1 );
59
- }
60
- return Math . max(longestCommonSubsequence(text1, i + 1 , text2, j), longestCommonSubsequence(text1, i, text2, j + 1 ));
61
- }
62
- }
63
- ```
64
-
65
- ## Solution 3
66
-
67
- Top-down DP
68
-
69
- ``` java
70
- /**
71
- * Question : 1143. Longest Common Subsequence
72
- * Complexity : Time: O(n^2) ; Space: O(n^2)
73
- * Topics : DP
74
- */
75
- public class Solution {
13
+ class Solution {
76
14
public int longestCommonSubsequence (String text1 , String text2 ) {
77
- Integer [][] dprize = new Integer [text1. length() + 1 ][text2. length() + 1 ];
78
- return longestCommonSubsequence(text1, 0 , text2, 0 , dprize);
15
+ int n1 = text1. length();
16
+ int n2 = text2. length();
17
+ return longestCommonSubsequenceUtil(text1, n1, text2, n2);
79
18
}
80
19
81
- private int longestCommonSubsequence (String text1 , int i , String text2 , int j , Integer [][] dprize ) {
82
- if (i >= text1 . length() || j >= text2 . length() ) {
20
+ private int longestCommonSubsequenceUtil (String text1 , int n1 , String text2 , int n2 ) {
21
+ if (n1 == 0 || n2 == 0 ) {
83
22
return 0 ;
84
23
}
85
24
86
- if (dprize[i][j] != null ) {
87
- return dprize[i][j];
88
- }
89
-
90
- if (text1. charAt(i) == text2. charAt(j)) {
91
- dprize[i][j] = 1 + longestCommonSubsequence(text1, i + 1 , text2, j + 1 , dprize);
25
+ if (text1. charAt(n1 - 1 ) == text2. charAt(n2 - 1 )) {
26
+ return 1 + longestCommonSubsequenceUtil(text1, n1 - 1 , text2, n2 - 1 );
92
27
} else {
93
- dprize[i][j] = Math . max(
94
- longestCommonSubsequence (text1, i + 1 , text2, j, dprize ),
95
- longestCommonSubsequence (text1, i , text2, j + 1 , dprize )
28
+ return Math . max(
29
+ longestCommonSubsequenceUtil (text1, n1 - 1 , text2, n2 ),
30
+ longestCommonSubsequenceUtil (text1, n1 , text2, n2 - 1 )
96
31
);
97
32
}
98
-
99
- return dprize[i][j];
100
33
}
101
34
}
102
35
```
103
36
104
- ## Solution 4
37
+ ## Solution 2
105
38
106
- Bottom-up DP
39
+ DP
107
40
108
41
``` java
109
42
/**
@@ -113,14 +46,14 @@ Bottom-up DP
113
46
*/
114
47
class Solution {
115
48
public int longestCommonSubsequence (String text1 , String text2 ) {
116
- if (text1. length() == 0 || text2. length() == 0 ) {
117
- return 0 ;
118
- }
49
+ int n1 = text1. length();
50
+ int n2 = text2. length();
119
51
120
- int [][] dp = new int [text1. length() + 1 ][text2. length() + 1 ];
52
+ // dp[i][j]: Longest common subsequence given i and j indexes.
53
+ int [][] dp = new int [n1 + 1 ][n2 + 1 ];
121
54
122
- for (int i = 1 ; i <= text1 . length() ; i++ ) {
123
- for (int j = 1 ; j <= text2 . length() ; j++ ) {
55
+ for (int i = 1 ; i <= n1 ; i++ ) {
56
+ for (int j = 1 ; j <= n2 ; j++ ) {
124
57
if (text1. charAt(i - 1 ) == text2. charAt(j - 1 )) {
125
58
dp[i][j] = 1 + dp[i - 1 ][j - 1 ];
126
59
} else {
@@ -129,7 +62,7 @@ class Solution {
129
62
}
130
63
}
131
64
132
- return dp[text1 . length()][text2 . length() ];
65
+ return dp[n1][n2 ];
133
66
}
134
67
}
135
68
```
0 commit comments