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++
#include<bits/stdc++.h>
using namespace std;
int maxPrefix( char s[], char t[])
{
int count = 0;
for ( int i = 0; i < strlen (t); i++)
{
if (count == strlen (s))
break ;
if (t[i] == s[count])
count++;
}
return count;
}
int main()
{
char S[] = "digger" ;
char T[] = "biggerdiagram" ;
cout << maxPrefix(S, T)
<< endl;
return 0;
}
|
Java
public class GFG {
static int maxPrefix(String s,
String t)
{
int count = 0 ;
for ( int i = 0 ; i < t.length(); i++)
{
if (count == s.length())
break ;
if (t.charAt(i) == s.charAt(count))
count++;
}
return count;
}
public static void main(String args[])
{
String S = "digger" ;
String T = "biggerdiagram" ;
System.out.println(maxPrefix(S, T));
}
}
|
Python 3
def maxPrefix(s, t) :
count = 0
for i in range ( 0 , len (t)) :
if (count = = len (s)) :
break
if (t[i] = = s[count]) :
count = count + 1
return count
S = "digger"
T = "biggerdiagram"
print (maxPrefix(S, T))
|
C#
using System;
class GFG
{
static int maxPrefix(String s,
String t)
{
int count = 0;
for ( int i = 0; i < t.Length; i++)
{
if (count == s.Length)
break ;
if (t[i] == s[count])
count++;
}
return count;
}
public static void Main()
{
String S = "digger" ;
String T = "biggerdiagram" ;
Console.Write(maxPrefix(S, T));
}
}
|
PHP
<?php
function maxPrefix( $s , $t )
{
$count = 0;
for ( $i = 0; $i < strlen ( $t ); $i ++)
{
if ( $count == strlen ( $s ))
break ;
if ( $t [ $i ] == $s [ $count ])
$count ++;
}
return $count ;
}
{
$S = "digger" ;
$T = "biggerdiagram" ;
echo maxPrefix( $S , $T ) ;
return 0;
}
?>
|
Javascript
<script>
function maxPrefix(s,t)
{
let count = 0;
for (let i = 0; i < t.length; i++)
{
if (count == s.length)
break ;
if (t[i] == s[count])
count++;
}
return count;
}
let S = "digger" ;
let T = "biggerdiagram" ;
document.write(maxPrefix(S, T));
</script>
|
Time complexity: O(n)
Space complexity: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!