Skip to content
Open
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions C/Algorithms/Dynamic-Programming/Longest_Common_Subsequence.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int l1,l2,i,j,check=0;
char *ar1=(char*)malloc(50*sizeof(char));
char *ar2=(char*)malloc(50*sizeof(char));
printf("enter string 1 and string 2\n");
scanf("%s %s",ar1,ar2);
l1=strlen(ar1);
l2=strlen(ar2);

//CREATING DP TABLE
int **arr=(int **)malloc((l1+1)*sizeof(int *));//2D array with constitutes the DP table
for(i=0;i<=l1;i++)
arr[i]=(int *)malloc((l2+1)*sizeof(int *));

//PREPROCESSING DP TABLE
for(i=0;i<=l1;i++)
arr[i][0]=0;
for(i=0;i<=l2;i++)
arr[0][i]=0;

//DP ITERATION STARTS
for(i=1;i<=l1;i++)
{
for(j=1;j<=l2;j++)
{
if(ar1[i-1]==ar2[j-1])
arr[i][j]=arr[i-1][j-1]+1;
else
{
if(arr[i-1][j]>arr[i][j-1])
arr[i][j]=arr[i-1][j];
else
arr[i][j]=arr[i][j-1];
}
}
}
//arr[l1][l2] contains the length of the longest common subsequence
printf("The length of Longest Common Subsequence is %d\n", arr[l1][l2]);

return 0;
}

5 changes: 5 additions & 0 deletions C/Algorithms/Dynamic-Programming/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
### Dynamic-Programming
- [Longest Common Subsequence](Longest_Common_Subsequence.c)

Given two sequences, find the length of longest subsequence present in both of them.

Time complexity = O(mn) where "m" is the length of the 1st sequence and "n" is the length of the 2nd sequence