June 13, 2022 |1.4K Views

    Maximum length prefix of one string that occurs as subsequence in another

      Share   Like
    Description
    Discussion

    Given two strings s and t. The task is to find maximum length of some prefix of the string S which occur in string t as subsequence.
    Examples : 
     

    Input : s = "digger"
                  t = "biggerdiagram"
    Output : 3
    digger
    biggerdiagram
    Prefix "dig" of s is longest subsequence in t.

    Input : s = "geeksforgeeks"
                  t = "agbcedfeitk"
    Output : 4

     

    A simple solutions is to consider all prefixes on by one and check if current prefix of s[] is a subsequence of t[] or not. Finally return length of the largest prefix.

    An efficient solution is based on the fact that to find a prefix of length n, we must first find the prefix of length n – 1 and then look for s[n-1] in t. Similarly, to find a prefix of length n – 1, we must first find the prefix of length n – 2 and then look for s[n – 2] and so on. 

    Maximum length prefix of one string that occurs as subsequence in another: https://www.geeksforgeeks.org/maximum-length-prefix-one-string-occurs-subsequence-another/