Maximum length prefix of one string that occurs as subsequence in another Last Updated : 20 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 one 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: 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 Code 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 == s.length()) break; // If character match, // increment counter. if (t.charAt(i) == s.charAt(count)) count++; } return count; } // Driver Code 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 Python 3 # Python 3 program to find maximum # length prefix of one string occur # as subsequence in another string. # Return the maximum length # prefix which is subsequence. def maxPrefix(s, t) : count = 0 # Iterating string T. for i in range(0,len(t)) : # If end of string S. if (count == len(s)) : break # If character match, # increment counter. if (t[i] == s[count]) : count = count + 1 return count # Driver Code S = "digger" T = "biggerdiagram" print(maxPrefix(S, T)) # This code is contributed # by Nikita Tiwari. C# // C# program to find maximum // length prefix of one string // occur as subsequence in // another string. using System; 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 == s.Length) break; // If character match, // increment counter. if (t[i] == s[count]) count++; } return count; } // Driver Code public static void Main() { String S = "digger"; String T = "biggerdiagram"; Console.Write(maxPrefix(S, T)); } } // This code is contributed by nitin mittal PHP <?php // PHP program to find maximum // length prefix of one string // occur as subsequence in another // string. // Return the maximum length // prefix which is subsequence. function maxPrefix($s, $t) { $count = 0; // Iterating string T. for ($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; } // Driver Code { $S = "digger"; $T = "biggerdiagram"; echo maxPrefix($S, $T) ; return 0; } // This code is contributed by nitin mittal. ?> JavaScript <script> // JavaScript program to find maximum // length prefix of one string // occur as subsequence in another // string. // Return the maximum length // prefix which is subsequence. function maxPrefix(s,t) { let count = 0; // Iterating string T. for (let i = 0; i < t.length; i++) { // If end of string S. if (count == s.length) break; // If character match, // increment counter. if (t[i] == s[count]) count++; } return count; } // Driver Code let S = "digger"; let T = "biggerdiagram"; document.write(maxPrefix(S, T)); </script> Output3 Time complexity: O(n) Space complexity: O(1) Maximum length prefix of one string that occurs as subsequence in another Comment More infoAdvertise with us Next Article KMP Algorithm for Pattern Searching K kartik Improve Article Tags : Strings Pattern Searching DSA subsequence Practice Tags : Pattern SearchingStrings Similar Reads What is Pattern Searching ? Pattern searching in Data Structures and Algorithms (DSA) is a fundamental concept that involves searching for a specific pattern or sequence of elements within a given data structure. This technique is commonly used in string matching algorithms to find occurrences of a particular pattern within a 5 min read Introduction to Pattern Searching - Data Structure and Algorithm Tutorial Pattern searching is an algorithm that involves searching for patterns such as strings, words, images, etc. We use certain algorithms to do the search process. The complexity of pattern searching varies from algorithm to algorithm. They are very useful when performing a search in a database. The Pat 15+ min read Naive algorithm for Pattern Searching Given text string with length n and a pattern with length m, the task is to prints all occurrences of pattern in text. Note: You may assume that n > m. Examples:Â Input: Â text = "THIS IS A TEST TEXT", pattern = "TEST"Output: Pattern found at index 10 Input: Â text = Â "AABAACAADAABAABA", pattern = 6 min read Rabin-Karp Algorithm for Pattern Searching Given two strings text and pattern string, your task is to find all starting positions where the pattern appears as a substring within the text. The strings will only contain lowercase English alphabets.While reporting the results, use 1-based indexing (i.e., the first character of the text is at po 12 min read KMP Algorithm for Pattern Searching Given two strings txt and pat, the task is to return all indices of occurrences of pat within txt. Examples:Input: txt = "abcab", pat = "ab"Output: [0, 3]Explanation: The string "ab" occurs twice in txt, first occurrence starts from index 0 and second from index 3.Input: txt= "aabaacaadaabaaba", pat 14 min read Z algorithm (Linear time pattern searching Algorithm) This algorithm efficiently locates all instances of a specific pattern within a text in linear time. If the length of the text is "n" and the length of the pattern is "m," then the total time taken is O(m + n), with a linear auxiliary space. It is worth noting that the time and auxiliary space of th 13 min read Finite Automata algorithm for Pattern Searching Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m.Examples: Input: txt[] = "THIS IS A TEST TEXT" pat[] = "TEST" Output: Pattern found at index 10 Input: txt[] = "AABAACAADAAB 13 min read Boyer Moore Algorithm for Pattern Searching Pattern searching is an important problem in computer science. When we do search for a string in a notepad/word file, browser, or database, pattern searching algorithms are used to show the search results. A typical problem statement would be-Â " Given a text txt[0..n-1] and a pattern pat[0..m-1] whe 15+ min read Aho-Corasick Algorithm for Pattern Searching Given an input text and an array of k words, arr[], find all occurrences of all words in the input text. Let n be the length of text and m be the total number of characters in all words, i.e. m = length(arr[0]) + length(arr[1]) + ... + length(arr[k-1]). Here k is total numbers of input words. Exampl 15+ min read ÂÂkasaiâs Algorithm for Construction of LCP array from Suffix Array Background Suffix Array : A suffix array is a sorted array of all suffixes of a given string. Let the given string be "banana". 0 banana 5 a1 anana Sort the Suffixes 3 ana2 nana ----------------> 1 anana 3 ana alphabetically 0 banana 4 na 4 na 5 a 2 nanaThe suffix array for "banana" :suffix[] = { 15+ min read Like