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 2bbffe7 commit 74f266dCopy full SHA for 74f266d
longest_common_subsequence.cpp
@@ -0,0 +1,25 @@
1
+class Solution {
2
+public:
3
+ int longestCommonSubsequence(string text1, string text2) {
4
+
5
+ int m = text1.size(), n = text2.size();
6
7
+ if(m == 0 or n == 0) return 0;
8
9
+ vector<vector<int>> dp(m+1, vector<int>(n+1));
10
+ for(int i=0; i<=m; i++){
11
+ for(int j=0; j<=n; j++){
12
+ if(i==0) dp[i][j] = 0;
13
+ else if(j==0) dp[i][j] = 0;
14
15
+ else{
16
+ if(text1[i-1] == text2[j-1])
17
+ dp[i][j] = dp[i-1][j-1] + 1;
18
+ else
19
+ dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
20
+ }
21
22
23
+ return dp[m][n];
24
25
+};
0 commit comments