Given two strings S1 and S2 of length N and M respectively, the task is to find the maximum value of times the string S2 needs to be concatenated, such that it is a substring of the string S1.
Examples:
Input: S1 = “ababc”, S2 = “ab”
Output: 2
Explanation: After concatenating S2 exactly twice, the string modifies to “abab”. Therefore, string “abab” is a substring of “ababc”. Therefore, the result is 2.
Input: S1 = “ababc”, S2 = “ba”
Output: 1
Explanation: String “ba” is already a substring of “ababc”. Therefore, the result is 1.
Approach: To solve the given problem, the idea to use the KMP algorithm. Follow the steps below to solve the problem:
- Initialize a variable, say ans, to store the maximum value K such that concatenating S2 K times form a substring of the string S1.
- Initialize a string curWord and copy the contents of S2 in curWord.
- Store the maximum number of times a string can occur as numWords = N / M.
- Traverse the string over the indices [0, numWords – 1] and perform the following steps:
- If the value of curWord is found in string S1, then increment ans by 1 and concatenate curWord with the word.
- Otherwise, break out of the loop.
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void computeLPSArray(string pat, int M,
int * lps)
{
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else {
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
}
int KMPSearch(string pat, string txt)
{
int M = pat.size();
int N = txt.size();
int lps[M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
return 1;
j = lps[j - 1];
}
else if (i < N
&& pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return 0;
}
void maxRepeating(string seq, string word)
{
int resCount = 0;
string curWord = word;
int numWords = seq.length() / word.length();
for ( int i = 0; i < numWords; i++) {
if (KMPSearch(curWord, seq)) {
curWord += word;
resCount++;
}
else
break ;
}
cout << resCount;
}
int main()
{
string S1 = "ababc" , S2 = "ab" ;
maxRepeating(S1, S2);
return 0;
}
|
Java
class GFG
{
static void computeLPSArray(String pat, int M,
int []lps)
{
int len = 0 ;
lps[ 0 ] = 0 ;
int i = 1 ;
while (i < M)
{
if (pat.charAt(i) == pat.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0 )
{
len = lps[len - 1 ];
}
else
{
lps[i] = 0 ;
i++;
}
}
}
}
static int KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
int lps[] = new int [M];
computeLPSArray(pat, M, lps);
int i = 0 ;
int j = 0 ;
while (i < N)
{
if (pat.charAt(j) == txt.charAt(i))
{
j++;
i++;
}
if (j == M)
{
return 1 ;
}
else if (i < N
&& pat.charAt(j) != txt.charAt(i))
{
if (j != 0 )
j = lps[j - 1 ];
else
i = i + 1 ;
}
}
return 0 ;
}
static void maxRepeating(String seq, String word)
{
int resCount = 0 ;
String curWord = word;
int numWords = seq.length() / word.length();
for ( int i = 0 ; i < numWords; i++)
{
if (KMPSearch(curWord, seq) == 1 )
{
curWord += word;
resCount++;
}
else
break ;
}
System.out.print(resCount);
}
public static void main (String[] args)
{
String S1 = "ababc" , S2 = "ab" ;
maxRepeating(S1, S2);
}
}
|
Python3
def computeLPSArray(pat, M, lps):
lenn = 0
lps[ 0 ] = 0
i = 1
while (i < M):
if (pat[i] = = pat[lenn]):
lenn + = 1
lps[i] = lenn
i + = 1
else :
if (lenn ! = 0 ):
lenn = lps[lenn - 1 ]
else :
lps[i] = 0
i + = 1
def KMPSearch(pat, txt):
M = len (pat)
N = len (txt)
lps = [ 0 for i in range (M)]
computeLPSArray(pat, M, lps)
i = 0
j = 0
while (i < N):
if (pat[j] = = txt[i]):
j + = 1
i + = 1
if (j = = M):
return 1
j = lps[j - 1 ]
elif (i < N and pat[j] ! = txt[i]):
if (j ! = 0 ):
j = lps[j - 1 ]
else :
i = i + 1
return 0
def maxRepeating(seq, word):
resCount = 0
curWord = word
numWords = len (seq) / / len (word)
for i in range (numWords):
if (KMPSearch(curWord, seq)):
curWord + = word
resCount + = 1
else :
break
print (resCount)
if __name__ = = '__main__' :
S1,S2 = "ababc" , "ab"
maxRepeating(S1, S2)
|
C#
using System;
class GFG
{
static void computeLPSArray(String pat, int M,
int []lps)
{
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M)
{
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}
static int KMPSearch(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
int []lps = new int [M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}
if (j == M)
{
return 1;
}
else if (i < N
&& pat[j] != txt[i])
{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return 0;
}
static void maxRepeating(String seq, String word)
{
int resCount = 0;
String curWord = word;
int numWords = seq.Length / word.Length;
for ( int i = 0; i < numWords; i++)
{
if (KMPSearch(curWord, seq) == 1)
{
curWord += word;
resCount++;
}
else
break ;
}
Console.Write(resCount);
}
public static void Main(String[] args)
{
String S1 = "ababc" , S2 = "ab" ;
maxRepeating(S1, S2);
}
}
|
Javascript
<script>
function computeLPSArray(pat, M, lps)
{
var len = 0;
lps[0] = 0;
var i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else {
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
}
function KMPSearch(pat, txt)
{
var M = pat.length;
var N = txt.length;
var lps = Array(M);
computeLPSArray(pat, M, lps);
var i = 0;
var j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
return 1;
j = lps[j - 1];
}
else if (i < N
&& pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return 0;
}
function maxRepeating(seq, word)
{
var resCount = 0;
var curWord = word;
var numWords = seq.length / word.length;
for ( var i = 0; i < numWords; i++) {
if (KMPSearch(curWord, seq)) {
curWord += word;
resCount++;
}
else
break ;
}
document.write( resCount);
}
var S1 = "ababc" , S2 = "ab" ;
maxRepeating(S1, S2);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(M)