Skip to content

Commit 00a441a

Browse files
authored
Merge pull request IIdroyII#34 from vedic-kalra/main
Longest Common Subsequence CPP Implementation
2 parents e9b34e7 + 89398c3 commit 00a441a

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//here i am providing two solutions to the following problems using dynamic programming
2+
//one is Top-Down DP and another is Bottom-Up DP
3+
// this paticular questions corresponds to the folloing question on leetcode
4+
//leetcode-1143 https://leetcode.com/problems/longest-common-subsequence/
5+
//here i have explained the question in a gradual optimal manner
6+
//------------------------------------------------------------------------------------------------------------------
7+
//brute force approach
8+
class Solution {
9+
public int longestCommonSubsequence(String text1, String text2) {
10+
return longestCommonSubsequence(text1, text2, 0, 0);
11+
}
12+
13+
private int longestCommonSubsequence(String text1, String text2, int i, int j) {
14+
if (i == text1.length() || j == text2.length())
15+
return 0;
16+
if (text1.charAt(i) == text2.charAt(j))
17+
return 1 + longestCommonSubsequence(text1, text2, i + 1, j + 1);
18+
else
19+
return Math.max(
20+
longestCommonSubsequence(text1, text2, i + 1, j),
21+
longestCommonSubsequence(text1, text2, i, j + 1)
22+
);
23+
}
24+
}
25+
//------------------------------------------------------------------------------------------------------------------
26+
/* Top-down DP
27+
We might use memoization to overcome overlapping subproblems.
28+
Since there are two changing values, i.e. i and j in the recursive
29+
function longestCommonSubsequence, we might apply a two-dimensional array.*/
30+
class Solution {
31+
private Integer[][] dp;
32+
public int longestCommonSubsequence(String text1, String text2) {
33+
dp = new Integer[text1.length()][text2.length()];
34+
return longestCommonSubsequence(text1, text2, 0, 0);
35+
}
36+
37+
private int longestCommonSubsequence(String text1, String text2, int i, int j) {
38+
if (i == text1.length() || j == text2.length())
39+
return 0;
40+
41+
if (dp[i][j] != null)
42+
return dp[i][j];
43+
44+
if (text1.charAt(i) == text2.charAt(j))
45+
return dp[i][j] = 1 + longestCommonSubsequence(text1, text2, i + 1, j + 1);
46+
else
47+
return dp[i][j] = Math.max(
48+
longestCommonSubsequence(text1, text2, i + 1, j),
49+
longestCommonSubsequence(text1, text2, i, j + 1)
50+
);
51+
}
52+
}
53+
//------------------------------------------------------------------------------------------------------------------
54+
/*Bottom-up DP
55+
For every i in text1, j in text2, we will choose one of the following two options:
56+
57+
if two characters match, length of the common subsequence would be 1 plus the
58+
length of the common subsequence till the i-1 andj-1 indexes
59+
if two characters doesn't match, we will take the longer by either skipping i or j indexes*/
60+
class Solution {
61+
public int longestCommonSubsequence(String text1, String text2) {
62+
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
63+
for (int i = 1; i <= text1.length(); i++) {
64+
for (int j = 1; j <= text2.length(); j++) {
65+
if (text1.charAt(i - 1) == text2.charAt(j - 1))
66+
dp[i][j] = 1 + dp[i - 1][j - 1];
67+
else
68+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
69+
}
70+
}
71+
return dp[text1.length()][text2.length()];
72+
}
73+
}

0 commit comments

Comments
 (0)