Find length of longest subsequence of one string which is substring of another string | Base Case ; if the last char of both strings are equal ; if the last char of both strings are not equal ; Driver code ; as minimum length can be 0 only . ; traversing for every length of Y .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubsequenceSubstring ( string & X , string & Y , int n , int m ) { if ( n == 0 m == 0 ) return 0 ; if ( X [ n - 1 ] == Y [ m - 1 ] ) { return 1 + maxSubsequenceSubstring ( X , Y , n - 1 , m - 1 ) ; } else { return maxSubsequenceSubstring ( X , Y , n - 1 , m ) ; } } int main ( ) { string X = " abcd " ; string Y = " bacdbdcd " ; int n = X . size ( ) , m = Y . size ( ) ; int maximum_length = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { int temp_ans = maxSubsequenceSubstring ( X , Y , n , i ) ; if ( temp_ans > maximum_length ) maximum_length = temp_ans ; } cout << " Length ▁ for ▁ maximum ▁ possible ▁ Subsequence ▁ of ▁ string ▁ X ▁ which ▁ is ▁ Substring ▁ of ▁ Y ▁ - > ▁ " << maximum_length ; return 0 ; }