Maximum number of times a given string needs to be concatenated to form a substring of another string
Last Updated :
31 May, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find lps[] for given
// pattern pat[0..M-1]
void computeLPSArray(string pat, int M,
int* lps)
{
// Length of the previous
// longest prefix suffix
int len = 0;
// lps[0] is always 0
lps[0] = 0;
// Iterate string to calculate lps[i]
int i = 1;
while (i < M) {
// If the current character
// of the pattern matches
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
// Otherwise
else {
if (len != 0) {
len = lps[len - 1];
}
// Otherwise
else {
lps[i] = 0;
i++;
}
}
}
}
// Function to implement KMP algorithm
int KMPSearch(string pat, string txt)
{
int M = pat.size();
int N = txt.size();
// Stores the longest prefix and
// suffix values for pattern
int lps[M];
// Preprocess the pattern and
// find the lps[] array
computeLPSArray(pat, M, lps);
// Index for txt[]
int i = 0;
// Index for pat[]
int j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
// If pattern is found return 1
if (j == M) {
return 1;
j = lps[j - 1];
}
// Mismatch after j matches
else if (i < N
&& pat[j] != txt[i]) {
// Don't match lps[0, lps[j - 1]]
// characters they will
// match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
// Return 0 if the pattern is not found
return 0;
}
// Function to find the maximum value
// K for which string S2 concatenated
// K times is a substring of string S1
void maxRepeating(string seq, string word)
{
// Store the required maximum number
int resCount = 0;
// Create a temporary string to store
// string word
string curWord = word;
// Store the maximum number of times
// string S2 can occur in string S1
int numWords = seq.length() / word.length();
// Traverse in range[0, numWords-1]
for (int i = 0; i < numWords; i++) {
// If curWord is found in sequence
if (KMPSearch(curWord, seq)) {
// Concatenate word to curWord
curWord += word;
// Increment resCount by 1
resCount++;
}
// Otherwise break the loop
else
break;
}
// Print the answer
cout << resCount;
}
// Driver Code
int main()
{
string S1 = "ababc", S2 = "ab";
// Function Call
maxRepeating(S1, S2);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to find lps[] for given
// pattern pat[0..M-1]
static void computeLPSArray(String pat, int M,
int []lps)
{
// Length of the previous
// longest prefix suffix
int len = 0;
// lps[0] is always 0
lps[0] = 0;
// Iterate string to calculate lps[i]
int i = 1;
while (i < M)
{
// If the current character
// of the pattern matches
if (pat.charAt(i) == pat.charAt(len))
{
len++;
lps[i] = len;
i++;
}
// Otherwise
else
{
if (len != 0)
{
len = lps[len - 1];
}
// Otherwise
else
{
lps[i] = 0;
i++;
}
}
}
}
// Function to implement KMP algorithm
static int KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
// Stores the longest prefix and
// suffix values for pattern
int lps[] = new int[M];
// Preprocess the pattern and
// find the lps[] array
computeLPSArray(pat, M, lps);
// Index for txt[]
int i = 0;
// Index for pat[]
int j = 0;
while (i < N)
{
if (pat.charAt(j) == txt.charAt(i))
{
j++;
i++;
}
// If pattern is found return 1
if (j == M)
{
return 1;
//j = lps[j - 1];
}
// Mismatch after j matches
else if (i < N
&& pat.charAt(j) != txt.charAt(i))
{
// Don't match lps[0, lps[j - 1]]
// characters they will
// match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
// Return 0 if the pattern is not found
return 0;
}
// Function to find the maximum value
// K for which string S2 concatenated
// K times is a substring of string S1
static void maxRepeating(String seq, String word)
{
// Store the required maximum number
int resCount = 0;
// Create a temporary string to store
// string word
String curWord = word;
// Store the maximum number of times
// string S2 can occur in string S1
int numWords = seq.length() / word.length();
// Traverse in range[0, numWords-1]
for (int i = 0; i < numWords; i++)
{
// If curWord is found in sequence
if (KMPSearch(curWord, seq) == 1)
{
// Concatenate word to curWord
curWord += word;
// Increment resCount by 1
resCount++;
}
// Otherwise break the loop
else
break;
}
// Print the answer
System.out.print(resCount);
}
// Driver Code
public static void main (String[] args)
{
String S1 = "ababc", S2 = "ab";
// Function Call
maxRepeating(S1, S2);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to find lps[] for given
# pattern pat[0..M-1]
def computeLPSArray(pat, M, lps):
# Length of the previous
# longest prefix suffix
lenn = 0
# lps[0] is always 0
lps[0] = 0
# Iterate string to calculate lps[i]
i = 1
while (i < M):
# If the current character
# of the pattern matches
if (pat[i] == pat[lenn]):
lenn += 1
lps[i] = lenn
i += 1
# Otherwise
else:
if (lenn != 0):
lenn = lps[lenn - 1]
# Otherwise
else:
lps[i] = 0
i += 1
# Function to implement KMP algorithm
def KMPSearch(pat, txt):
M = len(pat)
N = len(txt)
# Stores the longest prefix and
# suffix values for pattern
lps = [0 for i in range(M)]
# Preprocess the pattern and
# find the lps[] array
computeLPSArray(pat, M, lps)
# Index for txt[]
i = 0
# Index for pat[]
j = 0
while (i < N):
if (pat[j] == txt[i]):
j += 1
i += 1
# If pattern is found return 1
if (j == M):
return 1
j = lps[j - 1]
# Mismatch after j matches
elif (i < N and pat[j] != txt[i]):
# Don't match lps[0, lps[j - 1]]
# characters they will
# match anyway
if (j != 0):
j = lps[j - 1]
else:
i = i + 1
# Return 0 if the pattern is not found
return 0
# Function to find the maximum value
# K for which S2 concatenated
# K times is a subof S1
def maxRepeating(seq, word):
# Store the required maximum number
resCount = 0
# Create a temporary to store
# word
curWord = word
# Store the maximum number of times
# S2 can occur in S1
numWords = len(seq) // len(word)
# Traverse in range[0, numWords-1]
for i in range(numWords):
# If curWord is found in sequence
if (KMPSearch(curWord, seq)):
# Concatenate word to curWord
curWord += word
# Increment resCount by 1
resCount += 1
# Otherwise break the loop
else:
break
# Print the answer
print(resCount)
# Driver Code
if __name__ == '__main__':
S1,S2 = "ababc","ab"
# Function Call
maxRepeating(S1, S2)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find lps[] for given
// pattern pat[0..M-1]
static void computeLPSArray(String pat, int M,
int []lps)
{
// Length of the previous
// longest prefix suffix
int len = 0;
// lps[0] is always 0
lps[0] = 0;
// Iterate string to calculate lps[i]
int i = 1;
while (i < M)
{
// If the current character
// of the pattern matches
if (pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
// Otherwise
else
{
if (len != 0)
{
len = lps[len - 1];
}
// Otherwise
else
{
lps[i] = 0;
i++;
}
}
}
}
// Function to implement KMP algorithm
static int KMPSearch(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
// Stores the longest prefix and
// suffix values for pattern
int []lps = new int[M];
// Preprocess the pattern and
// find the lps[] array
computeLPSArray(pat, M, lps);
// Index for txt[]
int i = 0;
// Index for pat[]
int j = 0;
while (i < N)
{
if (pat[j] == txt[i])
{
j++;
i++;
}
// If pattern is found return 1
if (j == M)
{
return 1;
//j = lps[j - 1];
}
// Mismatch after j matches
else if (i < N
&& pat[j] != txt[i])
{
// Don't match lps[0, lps[j - 1]]
// characters they will
// match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
// Return 0 if the pattern is not found
return 0;
}
// Function to find the maximum value
// K for which string S2 concatenated
// K times is a substring of string S1
static void maxRepeating(String seq, String word)
{
// Store the required maximum number
int resCount = 0;
// Create a temporary string to store
// string word
String curWord = word;
// Store the maximum number of times
// string S2 can occur in string S1
int numWords = seq.Length / word.Length;
// Traverse in range[0, numWords-1]
for (int i = 0; i < numWords; i++)
{
// If curWord is found in sequence
if (KMPSearch(curWord, seq) == 1)
{
// Concatenate word to curWord
curWord += word;
// Increment resCount by 1
resCount++;
}
// Otherwise break the loop
else
break;
}
// Print the answer
Console.Write(resCount);
}
// Driver Code
public static void Main(String[] args)
{
String S1 = "ababc", S2 = "ab";
// Function Call
maxRepeating(S1, S2);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program for the above approach
// Function to find lps[] for given
// pattern pat[0..M-1]
function computeLPSArray(pat, M, lps)
{
// Length of the previous
// longest prefix suffix
var len = 0;
// lps[0] is always 0
lps[0] = 0;
// Iterate string to calculate lps[i]
var i = 1;
while (i < M) {
// If the current character
// of the pattern matches
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
// Otherwise
else {
if (len != 0) {
len = lps[len - 1];
}
// Otherwise
else {
lps[i] = 0;
i++;
}
}
}
}
// Function to implement KMP algorithm
function KMPSearch(pat, txt)
{
var M = pat.length;
var N = txt.length;
// Stores the longest prefix and
// suffix values for pattern
var lps = Array(M);
// Preprocess the pattern and
// find the lps[] array
computeLPSArray(pat, M, lps);
// Index for txt[]
var i = 0;
// Index for pat[]
var j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
// If pattern is found return 1
if (j == M) {
return 1;
j = lps[j - 1];
}
// Mismatch after j matches
else if (i < N
&& pat[j] != txt[i]) {
// Don't match lps[0, lps[j - 1]]
// characters they will
// match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
// Return 0 if the pattern is not found
return 0;
}
// Function to find the maximum value
// K for which string S2 concatenated
// K times is a substring of string S1
function maxRepeating(seq, word)
{
// Store the required maximum number
var resCount = 0;
// Create a temporary string to store
// string word
var curWord = word;
// Store the maximum number of times
// string S2 can occur in string S1
var numWords = seq.length / word.length;
// Traverse in range[0, numWords-1]
for (var i = 0; i < numWords; i++) {
// If curWord is found in sequence
if (KMPSearch(curWord, seq)) {
// Concatenate word to curWord
curWord += word;
// Increment resCount by 1
resCount++;
}
// Otherwise break the loop
else
break;
}
// Print the answer
document.write( resCount);
}
// Driver Code
var S1 = "ababc", S2 = "ab";
// Function Call
maxRepeating(S1, S2);
</script>
Â
Â
Â
Time Complexity: O(N2)
Auxiliary Space: O(M)
Â
Similar Reads
Generate a string whose all K-size substrings can be concatenated to form the given string Given a string str of size N and an integer K, the task is to generate a string whose substrings of size K can be concatenated to form the given string. Examples: Input: str = "abbaaa" K = 2 Output: abaa Explanation: All substring of size 2 of parent string "abaa" are "ab", "ba" and "aa". After conc
5 min read
Length of longest substring to be deleted to make a string equal to another string Given two strings str1 and str2, where str2 is a subsequence of str1, the task is to find the length of the longest substring of str1 which when removed, makes the strings str2 and str1 equal. Examples: Input: str1 = âprogrammingbloodsâ, str2 = âibloodsâ Output: 8 Explanation: Substrings to be remov
9 min read
Count of substrings of a string containing another given string as a substring Given two strings S and T, the task is to count the number of substrings of S that contains string T in it as a substring. Examples: Input: S = "dabc", T = "ab"Output: 4Explanation: Substrings of S containing T as a substring are: S[0, 2] = âdabâS[1, 2] = âabâS[1, 3] = âabcâS[0, 3] = âdabcâ Input: S
8 min read
Maximum number of set bits count in a K-size substring of a Binary String Given a binary string S of size N and an integer K. The task is to find the maximum number of set bit appears in a substring of size K. Examples: Input: S = "100111010", K = 3 Output: 3 Explanation: The substring "111" contains 3 set bits. Input:S = "0000000", K = 4 Output: 0 Explanation: S doesn't
10 min read
Find the number of ways to form a string of length N that can be rearranged to include S as a substring. Given an integer N and string S of size M, the task is to find the number of ways of forming string of length N such that it is possible to rearrange the string to have S as substring. Print the answer modulo 109 + 7. Note: All characters of string S are distinct. Examples: Input: N = 3, S = "abc"Ou
8 min read
Concatenate strings in any order to get Maximum Number of "AB" Given an array of strings of length N, it is allowed to concatenate them in any order. Find the maximum possible number of occurrences of 'AB' in the resulting string. Examples: Input : N = 4, arr={ "BCA", "BGGGA", "JKA", "BALB" } Output : 3 Concatenate them in the order JKA + BGGA + BCA + BALB and
9 min read
Count of substrings of a string containing another given string as a substring | Set 2 Given two strings S and T of length N and M respectively, the task is to count the number of substrings of S that contains the string T in it as a substring. Examples: Input: S = âdabcâ, T = âabâOutput: 4Explanation:Substrings of S containing T as a substring are: S[0, 2] = âdabâS[1, 2] = âabâS[1, 3
8 min read
Find number of times a string occurs as a subsequence in given string Given two strings, find the number of times the second string occurs in the first string, whether continuous or discontinuous.Examples: Input: string a = "GeeksforGeeks"string b = "Gks"Output: 4Explanation: The four strings are - (Check characters marked in bold)GeeksforGeeksGeeksforGeeksGeeksforGee
15+ min read
Reorder the given string to form a K-concatenated string Given a string S and an integer K. The task is to form a string T such that the string T is a reordering of the string S in a way that it is a K-Concatenated-String. A string is said to be a K-Concatenated-String if it contains exactly K copies of some string.For example, the string "geekgeek" is a
8 min read
Count characters to be shifted from the start or end of a string to obtain another string Given two strings A and B where string A is an anagram of string B. In one operation, remove the first or the last character of the string A and insert at any position in A. The task is to find the minimum number of such operations required to be performed to convert string A into string B. Examples
6 min read