Smallest String consisting of a String S exactly K times as a Substring
Last Updated :
15 Jul, 2025
Given a string S of length N and integer K, find the smallest length string which contains the string S as a sub string exactly K times.
Examples:
Input: S = "abba", K = 3
Output: abbabbabba
Explanation: The string "abba" occurs K times in the string abbabbabba, i.e. {abbabbabba, abbabbabba, abbabbabba}
Input: S = "geeksforgeeks", K = 3
Output: "geeksforgeeksforgeeksforgeeks"
Approach: To optimize the above approach, find the Longest Proper Prefix which is also a suffix for the given string S, and then generate a substring of S excluding the longest common prefix and add this substring to the answer exactly K - 1 times to the original string. Follow the below steps to solve the problem:
- Find the length of the longest proper prefix using KMP algorithm.
- Append substring S.substring(N-lps[N-1]) to S, exactly K-1 times.
- Print the answer.
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// KMP algorithm
int* kmp(string& s)
{
int n = s.size();
int* lps = new int[n];
lps[0] = 0;
int i = 1, len = 0;
while (i < n) {
if (s[i] == s[len]) {
len++;
lps[i] = len;
i++;
}
else {
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to return the required string
string findString(string& s, int k)
{
int n = s.length();
// Finding the longest proper prefix
// which is also suffix
int* lps = kmp(s);
// ans string
string ans = "";
string suff
= s.substr(0, n - lps[n - 1]);
for (int i = 0; i < k - 1; ++i) {
// Update ans appending the
// substring K - 1 times
ans += suff;
}
// Append the original string
ans += s;
// Returning min length string
// which contain exactly k
// substring of given string
return ans;
}
// Driver Code
int main()
{
int k = 3;
string s = "geeksforgeeks";
cout << findString(s, k) << endl;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// KMP algorithm
static int[] kmp(String s)
{
int n = s.length();
int[] lps = new int[n];
lps[0] = 0;
int i = 1, len = 0;
while (i < n)
{
if (s.charAt(i) == s.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to return the required string
static String findString(String s, int k)
{
int n = s.length();
// Finding the longest proper prefix
// which is also suffix
int[] lps = kmp(s);
// ans string
String ans = "";
String suff = s.substring(0, n - lps[n - 1]);
for(int i = 0; i < k - 1; ++i)
{
// Update ans appending the
// substring K - 1 times
ans += suff;
}
// Append the original string
ans += s;
// Returning min length string
// which contain exactly k
// substring of given string
return ans;
}
// Driver code
public static void main (String[] args)
{
int k = 3;
String s = "geeksforgeeks";
System.out.println(findString(s, k));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to implement
# the above approach
# KMP algorithm
def kmp(s):
n = len(s)
lps = [None] * n
lps[0] = 0
i, Len = 1, 0
while (i < n):
if (s[i] == s[Len]):
Len += 1
lps[i] = Len
i += 1
else:
if (Len != 0):
Len = lps[Len - 1]
else:
lps[i] = 0
i += 1
return lps
# Function to return the required string
def findString(s, k):
n = len(s)
# Finding the longest proper prefix
# which is also suffix
lps = kmp(s)
# ans string
ans = ""
suff = s[0: n - lps[n - 1] : 1]
for i in range(k - 1):
# Update ans appending the
# substring K - 1 times
ans += suff
# Append the original string
ans += s
# Returning min length string
# which contain exactly k
# substring of given string
return ans
# Driver code
k = 3
s = "geeksforgeeks"
print(findString(s, k))
# This code is contributed by divyeshrabadiya07
C#
// C# program to implement
// the above approach
using System;
class GFG{
// KMP algorithm
static int[] kmp(string s)
{
int n = s.Length;
int[] lps = new int[n];
lps[0] = 0;
int i = 1, len = 0;
while (i < n)
{
if (s[i] == s[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to return the required string
static string findString(string s, int k)
{
int n = s.Length;
// Finding the longest proper prefix
// which is also suffix
int[] lps = kmp(s);
// ans string
string ans = "";
string suff = s.Substring(0,
n - lps[n - 1]);
for(int i = 0; i < k - 1; ++i)
{
// Update ans appending the
// substring K - 1 times
ans += suff;
}
// Append the original string
ans += s;
// Returning min length string
// which contain exactly k
// substring of given string
return ans;
}
// Driver code
public static void Main (string[] args)
{
int k = 3;
string s = "geeksforgeeks";
Console.Write(findString(s, k));
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// KMP algorithm
function kmp(s)
{
var n = s.length;
var lps = new Array(n);
lps[0] = 0;
var i = 1;
var len = 0;
while (i < n) {
if (s[i] == s[len]) {
len++;
lps[i] = len;
i++;
}
else {
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to return the required string
function findString(s, k)
{
var n = s.length;
// Finding the longest proper prefix
// which is also suffix
var lps = kmp(s);
// ans string
var ans = "";
var suff = s.substr(0, n - lps[n - 1]);
for (var i = 0; i < k - 1; ++i) {
// Update ans appending the
// substring K - 1 times
ans += suff;
}
// Append the original string
ans += s;
// Returning min length string
// which contain exactly k
// substring of given string
return ans;
}
// Driver Code
var k = 3;
var s = "geeksforgeeks";
document.write(findString(s, k));
//This code is contributed by phasing17
</script>
Outputgeeksforgeeksforgeeksforgeeks
Time Complexity: O( |S| ), where S is the given string
Auxiliary Space: O( |S| )
Similar Reads
Count M-length substrings occurring exactly K times in a string Given a string S of length N and two integers M and K, the task is to count the number of substrings of length M occurring exactly K times in the string S. Examples: Input: S = "abacaba", M = 3, K = 2Output: 1Explanation: All distinct substrings of length 3 are "aba", "bac", "aca", "cab".Out of all
15+ min read
Lexicographically smallest string formed by reversing Substrings of string S exactly K times Given a string S and an integer K, the task is to find the lexicographically smallest string possible after reversing any substring of any length exactly K times. Examples: Input: S = "fgazcbdfge", K = 3Output: abcdgfzfgeExplanation: After 1st operation: S = "agfzcbdfge", in S select S[0 - 2] = "fga
11 min read
Minimum size lexicographically smallest string which is not a substring of given string Given a string s, the task is to find the lexicographically smallest string of minimum characters that do not exist as a substring in S. Examples: Input: S = "aabacdefghijklmnopqrstuvwxyz"Output: adExplanation: All the single digit strings from [a-z] occur in the given string and in two character st
7 min read
K-th lexicographically smallest unique substring of a given string Given a string S. The task is to print the K-th lexicographically the smallest one among the different substrings of s.A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z, and an empty stri
5 min read
Smallest window in a String containing all characters of other String Given two strings s (length m) and p (length n), the task is to find the smallest substring in s that contains all characters of p, including duplicates. If no such substring exists, return "-1". If multiple substrings of the same length are found, return the one with the smallest starting index.Exa
15+ min read
Smallest window in a String containing all characters of other String Given two strings s (length m) and p (length n), the task is to find the smallest substring in s that contains all characters of p, including duplicates. If no such substring exists, return "-1". If multiple substrings of the same length are found, return the one with the smallest starting index.Exa
15+ min read