Skip to content

Commit

Permalink
Added Longest common subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
sksinghl498 committed Oct 21, 2021
1 parent f60858f commit a94cbc9
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions C/Algorithms/Dynamic-Programming/LonestCommonSubsequence.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<stdio.h>
#include<stdlib.h>
int max(int a,int b){
return (a>b)? a: b;
}
int LCS(char * a,char * b,int n1,int n2){
if(n1==0 || n2==0) return 0;

// if two character are common than check for subproblems
if(a[n1-1]==b[n2-1])
return 1+LCS(a,b,n1-1,n2-1);
// If two character is not common than two cases are arises
// case 1: to leave the character of first string and check for subproblem with second string
// case 2: to leave the character of second string and check for subproblem with first string
return max(LCS(a,b,n1-1,n2),LCS(a,b,n1,n2-1));
}
int main(){
int n1,n2; scanf("%d %d",&n1,&n2);

char a[n1],b[n2];
scanf("%s",a);
scanf("%s",b);
printf("longest common subsequence is %d\n",LCS(a,b,n1,n2));
return 0;
}

0 comments on commit a94cbc9

Please sign in to comment.