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

1.7

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.
Thus, we keep a counter which stores the current length of prefix found. We initialize it with 0 and begin with the first letter of s and keep iterating over t to find the occurrence of the first letter. As soon as we encounter the first letter of s we we update the counter and look for second letter. We keep updating the counter and looking for next letter, until either the string s is found or there are no more letters in t.

Below is the implementation of this approach:

C++

// C++ program to find maximum length prefix
// of one string occur as subsequence in another
// string.
#include<bits/stdc++.h>
using namespace std;

// Return the maximum length prefix which is
// subsequence.
int maxPrefix(char s[], char t[])
{
    int count = 0;

    // Iterating string T.
    for (int i = 0; i < strlen(t); i++)
    {
        // If end of string S.
        if (count == strlen(s))
            break;

        // If character match, increment counter.
        if (t[i] == s[count])
            count++;
    }

    return count;
}

// Driven Program
int main()
{
    char S[] = "digger";
    char T[] = "biggerdiagram";

    cout << maxPrefix(S, T) << endl;

    return 0;
}

Java

// Java program to find maximum length prefix
// of one string occur as subsequence in another
// string.
public class GFG {     
    
    // Return the maximum length prefix which is
    // subsequence.
    static int maxPrefix(String s, String t)
    {
        int count = 0;
     
        // Iterating string T.
        for (int i = 0; i < t.length(); i++)
        {
            // If end of string S.
            if (count == t.length())
                break;
     
            // If character match, increment 
            // counter.
            if (t.charAt(i) == s.charAt(count))
                count++;
        }
     
        return count;
    }
     
    // Driven Program
    public static void main(String args[])
    {
        String S = "digger";
        String T = "biggerdiagram";
     
        System.out.println(maxPrefix(S, T));
    }
}
// This code is contributed by Sumit Ghosh


Output:

3

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.7 Average Difficulty : 1.7/5.0
Based on 10 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.