Skip to content

longestCommonSubstring.cpp created #594

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

Merged
merged 1 commit into from
Oct 5, 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
56 changes: 56 additions & 0 deletions Dynamic Programming/longestCommonSubstring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Problem : A substring of a string is a subsequence in which all the characters are consecutive.Given two strings,
// we need to find the longest common substring.

// Approach :

// We are creating a 2d dp array corresponding to the dimensions of the array. Now if the characters for a pair of
// i, j are not equal, that show that they can be included in the final answer and hence they are discarded.
// Else, we take a look at dp[i-1][j-1] if the continuity is maintained the we can find a continous equal substring
// or not.

#include <bits/stdc++.h>
using namespace std;

int lcs(string &s1, string &s2)
{
int n = s1.size();
int m = s2.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (s1[i - 1] == s2[j - 1])
{
// looking if just previous characters are also equal or not
int val = 1 + dp[i - 1][j - 1];
dp[i][j] = val;
ans = max(ans, val);
}
else
{
// not a part of equal substring, hence 0
dp[i][j] = 0;
}
}
}
return ans;
}

int main()
{
string s1;
string s2;
cin >> s1 >> s2;
cout << "The Length of Longest Common Substring is " << lcs(s1, s2);
}

// Sample Inputs

// abcjklp
// acjkp

// Corresponding Outputs

// 3