Skip to content

Commit 654672a

Browse files
committed
add leetcode 1143.cpp
1 parent 746b866 commit 654672a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

leetcode/1143.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int longestCommonSubsequence(string text1, string text2) {
4+
vector<vector<int>> dp(text1.size()+1, vector<int>(text2.size()+1, 0));
5+
// dp[i][j] = LCS of text1[0..i] and text2[0..j]
6+
for (int i = 1; i <= text1.size(); ++i) {
7+
for (int j = 1; j <= text2.size(); ++j) {
8+
if (text1[i-1] == text2[j-1]) {
9+
dp[i][j] = dp[i-1][j-1] + 1;
10+
} else {
11+
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
12+
}
13+
}
14+
}
15+
16+
return dp[text1.size()][text2.size()];
17+
}
18+
};

0 commit comments

Comments
 (0)