Skip to content

Commit 74f266d

Browse files
Added solution
1 parent 2bbffe7 commit 74f266d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

longest_common_subsequence.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)