Skip to content
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
38 changes: 38 additions & 0 deletions LongestCommonSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;

//finding max value
int max(int a, int b)
{
return (a > b) ? a : b;
}

//finding LCS
int LongestCommonSubsequence(char *str1, char *str2, int x, int y)
{
if (x == 0 || y == 0)
{
return 0;
}
if (str1[x - 1] == str2[y - 1])
{
return (1 + LongestCommonSubsequence(str1, str2, x - 1, y - 1));
}
else
{
return max(LongestCommonSubsequence(str1, str2, x, y - 1), LongestCommonSubsequence(str1, str2, x - 1, y));
}
}

//main function
int main()
{
char str1[] = "cantonment";
char str2[] = "longatone";
int x = strlen(str1);
int y = strlen(str2);
cout << "\nLength of Longest Common Subsequence From Both The Strings Is " << LongestCommonSubsequence(str1, str2, x, y) << endl;
return 0;
}