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.
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 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:
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find length of longest subsequence of one string which is substring of another string
- Minimize length of prefix of string S containing all characters of another string T
- Find number of times a string occurs as a subsequence in given string
- Lexicographically largest subsequence such that every character occurs at least k times
- Longest subsequence where each character occurs at least k times
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Maximum length Subsequence with alternating sign and maximum Sum
- Length of longest substring to be deleted to make a string equal to another string
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Maximize length of Non-Decreasing Subsequence by reversing at most one Subarray
- Queries to print the character that occurs the maximum number of times in a given range
- Check if the string contains consecutive letters and each letter occurs exactly once
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Number of times the given string occurs in the array in the range [l, r]
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Transform One String to Another using Minimum Number of Given Operation
- Print array of strings in sorted order without copying one string into another
- Check if it is possible to convert one string into another with given constraints
- Check if it is possible to transform one string to another