-
Notifications
You must be signed in to change notification settings - Fork 0
/
1143_Longest-Common-Subsequence.cpp
47 lines (40 loc) · 1.31 KB
/
1143_Longest-Common-Subsequence.cpp
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
class Solution {
public:
int tabulation_sol(string text1, string text2) {
// Tabulation(bottom-up)
int len1 = text1.length();
int len2 = text2.length();
vector<vector<int>> dp(len1+1, vector<int>(len2+1, 0));
for(int i = len1 - 1; i >= 0; --i){
for(int j = len2 - 1; j >= 0; --j){
if(text1[i] == text2[j]){
dp[i][j] = 1 + dp[i+1][j+1];
}else{
dp[i][j] = max(dp[i+1][j], dp[i][j+1]);
}
}
}
return dp[0][0];
}
int memo_sol(string text1, string text2){
// Memoization(top-down)
int len1 = text1.length();
int len2 = text2.length();
vector<vector<int>> dp(len1+1, vector<int>(len2+1, 0));
// compare text1[i] and text2[j]
for(int i=1; i<=len1; ++i){
for(int j=1; j<=len2; ++j){
if(text1[i-1] == text2[j-1]){
dp[i][j] = 1 + dp[i-1][j-1];
}else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[len1][len2];
}
int longestCommonSubsequence(string text1, string text2) {
// return tabulation_sol(text1, text2);
return memo_sol(text1, text2);
}
};