Skip to content

Commit 2b11869

Browse files
add 1143
1 parent 2b3c88e commit 2b11869

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ _If you like this project, please leave me a star._ ★
344344
|1152|[Analyze User Website Visit Pattern](https://leetcode.com/problems/analyze-user-website-visit-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1152.java) |[:tv:](https://youtu.be/V510Lbtrm5s) |Medium|HashTable, Sort, Array|
345345
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java)|[:tv:](https://youtu.be/-t2cdVs9cKk) |Easy||
346346
|1146|[Snapshot Array](https://leetcode.com/problems/snapshot-array/)|[Javascript](./javascript/_1146.js)| | Easy ||
347+
|1143|[Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1143.java) | |Medium||String, DP
347348
|1138|[Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1138.java)| [:tv:](https://youtu.be/rk-aB4rEOyU)| Medium |HashTable, String|
348349
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | |Easy||
349350
|1136|[Parallel Courses](https://leetcode.com/problems/parallel-courses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1136.java) | |Medium||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1143 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/longest-common-subsequence/solution/
7+
* <p>
8+
* Recall that there are two different techniques we can use to implement a dynamic programming solution; memoization and tabulation.
9+
* <p>
10+
* Memoization is where we add caching to a function (that has no side effects). In dynamic programming, it is typically used on recursive functions for a top-down solution that starts with the initial problem and then recursively calls itself to solve smaller problems.
11+
* Tabulation uses a table to keep track of subproblem results and works in a bottom-up manner: solving the smallest subproblems before the large ones, in an iterative manner. Often, people use the words "tabulation" and "dynamic programming" interchangeably.
12+
* <p>
13+
* For most people, it's easiest to start by coming up with a recursive brute-force solution and then adding memoization to it. After that, they then figure out how to convert it into an (often more desired) bottom-up tabulated algorithm.
14+
*/
15+
public int longestCommonSubsequence(String text1, String text2) {
16+
if (text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0) {
17+
return 0;
18+
}
19+
int m = text1.length();
20+
int n = text2.length();
21+
int[][] dp = new int[m + 1][n + 1];
22+
for (int i = 0; i < m; i++) {
23+
for (int j = 0; j < n; j++) {
24+
dp[i][j] = -1;
25+
}
26+
}
27+
return topDownRecursiveSolve(dp, 0, 0, text1, text2);
28+
}
29+
30+
private int topDownRecursiveSolve(int[][] dp, int i, int j, String text1, String text2) {
31+
if (dp[i][j] != -1) {
32+
return dp[i][j];
33+
}
34+
//option1: we don't include text1.charAt(i) in the optimal solution
35+
int option1 = topDownRecursiveSolve(dp, i + 1, j, text1, text2);
36+
//option2: we do include text1.charAt(i) in the optimal solution as long as a match in text2 at or after j does exist
37+
int firstOccurence = text2.indexOf(text1.charAt(i), j);
38+
int option2 = 0;
39+
if (firstOccurence != -1) {
40+
option2 = 1 + topDownRecursiveSolve(dp, i + 1, firstOccurence + 1, text1, text2);
41+
}
42+
dp[i][j] = Math.max(option1, option2);
43+
return dp[i][j];
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1143;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1143Test {
10+
private static _1143.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1143.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.longestCommonSubsequence("abcde", "ace"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.longestCommonSubsequence("abc", "abc"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0, solution1.longestCommonSubsequence("abc", "def"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(2, solution1.longestCommonSubsequence("ezupkr", "ubmrapg"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)