From 466088c65b0462d9e45fc085cc70ed2d461bd6e1 Mon Sep 17 00:00:00 2001 From: bpsingh10 <44584648+bpsingh10@users.noreply.github.com> Date: Fri, 2 Oct 2020 10:57:36 +0530 Subject: [PATCH] Create LongestCommonSubstringLength.cpp --- Recursion DP/LongestCommonSubstringLength.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Recursion DP/LongestCommonSubstringLength.cpp diff --git a/Recursion DP/LongestCommonSubstringLength.cpp b/Recursion DP/LongestCommonSubstringLength.cpp new file mode 100644 index 0000000..ee5127b --- /dev/null +++ b/Recursion DP/LongestCommonSubstringLength.cpp @@ -0,0 +1,41 @@ +// A dynamic programming CPP program to find +// Length of Longest Common Subtring of two strings. +#include +using namespace std; + +int lcs(const string &s1, const string &s2) +{ + vector> dp(s1.length() + 1, vector(s2.length() + 1)); + int maxLength = 0; + for (int i = 1; i <= s1.length(); i++) + { + for (int j = 1; j <= s2.length(); j++) + { + if (s1[i - 1] == s2[j - 1]) + { + dp[i][j] = 1 + dp[i - 1][j - 1]; + maxLength = max(maxLength, dp[i][j]); + } + } + } + return maxLength; +} + +// Driver Code +int main() +{ + string str1, str2; + cout << "Enter First String: "; + cin >> str1; + cout << "Enter Second String: "; + cin >> str2; + int m = str1.size(); + int n = str2.size(); + + cout << "Length of LCS is " + << lcs( str1, str2, m, n ); + + return 0; +} + +// This code is contributed by Bhanu Pratap Singh