Skip to content

Commit 7719b8e

Browse files
committed
update: longest-common-subsequence
1 parent 107bab8 commit 7719b8e

File tree

1 file changed

+20
-87
lines changed
  • dynamic-programming/longest-common-subsequence

1 file changed

+20
-87
lines changed

dynamic-programming/longest-common-subsequence/README.md

Lines changed: 20 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,39 @@
44

55
DFS
66

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-
417
```java
428
/**
439
* Question : 1143. Longest Common Subsequence
4410
* Complexity : Time: O(2^n) ; Space: O(n)
4511
* Topics : DP
4612
*/
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 {
7614
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);
7918
}
8019

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) {
8322
return 0;
8423
}
8524

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);
9227
} 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)
9631
);
9732
}
98-
99-
return dprize[i][j];
10033
}
10134
}
10235
```
10336

104-
## Solution 4
37+
## Solution 2
10538

106-
Bottom-up DP
39+
DP
10740

10841
```java
10942
/**
@@ -113,14 +46,14 @@ Bottom-up DP
11346
*/
11447
class Solution {
11548
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();
11951

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];
12154

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++) {
12457
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
12558
dp[i][j] = 1 + dp[i - 1][j - 1];
12659
} else {
@@ -129,7 +62,7 @@ class Solution {
12962
}
13063
}
13164

132-
return dp[text1.length()][text2.length()];
65+
return dp[n1][n2];
13366
}
13467
}
13568
```

0 commit comments

Comments
 (0)