We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 746b866 commit 654672aCopy full SHA for 654672a
leetcode/1143.cpp
@@ -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