Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create LongestCommonSubsequence.cpp #2084

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions LongestCommonSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Longest Common Subsequence

#include<bits/stdc++.h>

using namespace std;


//recursion approach
int solve(string s1, int i, string s2, int j)
{
if(i==s1.length() || j==s2.length())return 0;
if(s1[i]==s2[j])
{
return 1 + solve(s1, i+1, s2, j+1);
}
else
{
return max(solve(s1, i+1, s2, j), solve(s1, i, s2, j+1));
}
}


//dp approach
int solve2(string s1, string s2)
{
int dp[s2.length()+1][s1.length()+1];
for(int i=0; i<=s2.length(); i++)
{
for(int j=0; j<=s1.length(); j++)
{
if(i==0 || j==0)dp[i][j]=0;
else if(s2[i-1]==s1[j-1]) dp[i][j]=1+dp[i-1][j-1];
else dp[i][j]=max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[s2.length()][s1.length()];
}


int main()
{
string s1, s2;
cin>>s1>>s2;
// cout<<solve(s1, 0, s2, 0)<<endl;
cout<<solve2(s1, s2)<<endl;
return 0;

}