-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
185 lines (173 loc) · 6.41 KB
/
Copy pathLongestCommonSubsequence.java
File metadata and controls
185 lines (173 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package algorithms;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* 1143. Longest Common Subsequence
* https://leetcode.com/problems/longest-common-subsequence/
* Difficulty : Medium
* Related Topics : String, Dynamic Programming
*
* Given two strings text1 and text2, return the length of their longest common subsequence.
* If there is no common subsequence, return 0.
*
* A subsequence of a string is a new string generated from the original string
* with some characters (can be none) deleted without changing the relative order of the remaining characters.
*
* For example, "ace" is a subsequence of "abcde".
* A common subsequence of two strings is a subsequence that is common to both strings.
*
*
*
* Example 1:
*
* Input: text1 = "abcde", text2 = "ace"
* Output: 3
* Explanation: The longest common subsequence is "ace" and its length is 3.
* Example 2:
*
* Input: text1 = "abc", text2 = "abc"
* Output: 3
* Explanation: The longest common subsequence is "abc" and its length is 3.
* Example 3:
*
* Input: text1 = "abc", text2 = "def"
* Output: 0
* Explanation: There is no such common subsequence, so the result is 0.
*
*
* Constraints:
*
* 1 <= text1.length, text2.length <= 1000
* text1 and text2 consist of only lowercase English characters.
*
* created by Cenk Canarslan on 2021-11-17
*/
public class LongestCommonSubsequence {
@Test
public void testLongestCommonSubsequence() {
/**
* Longest Common Subsequence with Recursion
* Be careful with long strings
*/
assertEquals(3, lcsWithRecursion("abcde", "ace"));
assertEquals(3, lcsWithRecursion("abc", "abc"));
assertEquals(0, lcsWithRecursion("abc", "def"));
assertEquals(2, lcsWithRecursion("aab", "azb"));
assertEquals(2, lcsWithRecursion("bd", "abcd"));
assertEquals(4, lcsWithRecursion("aggtab", "gxtxayb"));
// Takes ~20 seconds !!!!
// assertEquals(4, lcsWithRecursion("pmjghexybyrgzczy", "hafcdqbgncrcbihkd"));
/**
* Longest Common Subsequence with Recursion & Memoization
*/
assertEquals(3, lcsWithRecursionAndMemoization("abcde", "ace"));
assertEquals(3, lcsWithRecursionAndMemoization("abc", "abc"));
assertEquals(0, lcsWithRecursionAndMemoization("abc", "def"));
assertEquals(2, lcsWithRecursionAndMemoization("aab", "azb"));
assertEquals(2, lcsWithRecursionAndMemoization("bd", "abcd"));
assertEquals(4, lcsWithRecursionAndMemoization("aggtab", "gxtxayb"));
// Takes 2.2 milliseconds
assertEquals(4, lcsWithRecursionAndMemoization("pmjghexybyrgzczy", "hafcdqbgncrcbihkd"));
/**
* Longest Common Subsequence with Dynamic Programming
*/
assertEquals(3, lcsWithDynamicProgramming("abcde", "ace"));
assertEquals(3, lcsWithDynamicProgramming("abc", "abc"));
assertEquals(0, lcsWithDynamicProgramming("abc", "def"));
assertEquals(2, lcsWithDynamicProgramming("aab", "azb"));
assertEquals(2, lcsWithDynamicProgramming("bd", "abcd"));
assertEquals(4, lcsWithDynamicProgramming("aggtab", "gxtxayb"));
// Takes 0.18 milliseconds :)
assertEquals(4, lcsWithDynamicProgramming("pmjghexybyrgzczy", "hafcdqbgncrcbihkd"));
}
public static void main(String[] args) {
LongestCommonSubsequence lcs = new LongestCommonSubsequence();
// String s1 = "bd";
// String s2 = "abcd";
// String s1 = "aggtab";
// String s2 = "gxtxayb";
String s1 = "pmjghexybyrgzczy";
String s2 = "hafcdqbgncrcbihkd";
long start = System.nanoTime();
// int result = lcs.lcsWithRecursion(s1, s2);
// int result = lcs.lcsWithRecursionAndMemoization(s1, s2);
int result = lcs.lcsWithDynamicProgramming(s1, s2);
long end = System.nanoTime();
System.out.println("result = " + result + ", time = " + (end - start));
}
/**
* Dynamic Programming
*
* Runtime: 10 ms, faster than 72.06% of Java online submissions for Longest Common Subsequence.
* Memory Usage: 42.8 MB, less than 63.83% of Java online submissions for Longest Common Subsequence.
*
* @param s1
* @param s2
* @return
*/
public int lcsWithDynamicProgramming(String s1, String s2) {
int[][] dpTable = new int[s1.length()+1][s2.length()+1];
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
if (s1.charAt(i-1) == s2.charAt(j-1)) {
dpTable[i][j] = 1 + dpTable[i-1][j-1];
} else {
dpTable[i][j] = Math.max(dpTable[i-1][j], dpTable[i][j-1]);
}
}
}
return dpTable[s1.length()][s2.length()];
}
/**
* Recursive with memoization
*
* Runtime: 29 ms, faster than 14.11% of Java online submissions for Longest Common Subsequence.
* Memory Usage: 43.5 MB, less than 17.18% of Java online submissions for Longest Common Subsequence.
*
* @param s1
* @param s2
* @return
*/
public int lcsWithRecursionAndMemoization(String s1, String s2) {
int[][] memo = new int[s1.length()][s2.length()];
return sub(s1, s2, 0, 0, memo);
}
private int sub(String s1, String s2, int i, int j, int[][] memo) {
if (i == s1.length() || j == s2.length()) {
return 0;
}
if (memo[i][j] > 0) {
return memo[i][j];
}
int result;
if (s1.charAt(i) == s2.charAt(j)) {
result = 1 + sub(s1, s2, i+1, j+1, memo);
} else {
result = Math.max(sub(s1, s2, i, j+1, memo),
sub(s1, s2, i+1, j, memo));
}
memo[i][j] = result;
return result;
}
/**
* Recursive
*
* Time limit exceeded !!
*
* @param s1
* @param s2
* @return
*/
public int lcsWithRecursion(String s1, String s2) {
int s1Len = s1.length();
int s2Len = s2.length();
if (s1.isEmpty() || s2.isEmpty()) {
return 0;
} else if (s1.charAt(s1Len - 1) == s2.charAt(s2Len - 1)) {
return 1 + lcsWithRecursion(s1.substring(0, s1Len - 1), s2.substring(0, s2Len - 1));
} else {
return Math.max(lcsWithRecursion(s1.substring(0, s1Len - 1), s2),
lcsWithRecursion(s1, s2.substring(0, s2Len - 1)));
}
}
}