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 .
import java . util . * ; class GFG { static int maxSubsequenceSubString ( String X , String Y , int n , int m ) { if ( n == 0 m == 0 ) return 0 ; if ( X . charAt ( n - 1 ) == Y . charAt ( m - 1 ) ) { return 1 + maxSubsequenceSubString ( X , Y , n - 1 , m - 1 ) ; } else { return maxSubsequenceSubString ( X , Y , n - 1 , m ) ; } } public static void main ( String [ ] args ) { String X = " abcd " ; String Y = " bacdbdcd " ; int n = X . length ( ) , m = Y . length ( ) ; 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 ; } System . out . print ( " Length ▁ for ▁ maximum ▁ possible ▁ Subsequence ▁ of ▁ String ▁ X ▁ which ▁ is ▁ SubString ▁ of ▁ Y - > " + maximum_length ) ; } }