From a56f80029af58b802031056304e6869df8361337 Mon Sep 17 00:00:00 2001 From: Sahil Kandhare <104154041+SahilK-027@users.noreply.github.com> Date: Sun, 1 Jan 2023 20:24:24 +0530 Subject: [PATCH] Create Longest-common-substring.cpp --- .../Recursion/Longest-common-substring.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 algorithms/CPlusPlus/Recursion/Longest-common-substring.cpp diff --git a/algorithms/CPlusPlus/Recursion/Longest-common-substring.cpp b/algorithms/CPlusPlus/Recursion/Longest-common-substring.cpp new file mode 100644 index 00000000..5cf26a35 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/Longest-common-substring.cpp @@ -0,0 +1,27 @@ +// Given two strings ‘X’ and ‘Y’, find the length of the longest common substring. +// Executable link : https://ide.geeksforgeeks.org/e3ce1154-9585-44aa-8f4b-d499dc64f9cc + +#include +using namespace std ; +int solve(string s1, string s2, int ans, int i = 0, int j = 0){ + if(i == s1.length() || j == s2.length()){ + return ans; + } + // Match case + if(s1[i] == s2[j]){ + ans = 1 + solve(s1, s2,ans,i + 1, j+1); + } + + // Not match + else{ + ans = max(solve(s1, s2,ans,i, j+1), solve(s1, s2,ans,i + 1, j)); + } + return ans; +} +int main() +{ + string s1 = "abcde"; + string s2 = "abcde"; + cout<< solve(s1, s2, 0)<