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.
Below is the implementation of the above approach.
C++
// C++ Program to implement// the above approach#include <bits/stdc++.h>usingnamespacestd;// KMP algorithmint* kmp(string& s){intn = s.size();int* lps =newint[n];lps[0] = 0;inti = 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++;}}}returnlps;}// Function to return the required stringstring findString(string& s,intk){intn = s.length();// Finding the longest proper prefix// which is also suffixint* lps = kmp(s);// ans stringstring ans ="";string suff= s.substr(0, n - lps[n - 1]);for(inti = 0; i < k - 1; ++i) {// Update ans appending the// substring K - 1 timesans += suff;}// Append the original stringans += s;// Returning min length string// which contain exactly k// substring of given stringreturnans;}// Driver Codeintmain(){intk = 3;string s ="geeksforgeeks";cout << findString(s, k) << endl;}Java
// Java program to implement// the above approachimportjava.util.*;classGFG{// KMP algorithmstaticint[] kmp(String s){intn = s.length();int[] lps =newint[n];lps[0] =0;inti =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++;}}}returnlps;}// Function to return the required stringstaticString findString(String s,intk){intn = s.length();// Finding the longest proper prefix// which is also suffixint[] lps = kmp(s);// ans stringString ans ="";String suff = s.substring(0, n - lps[n -1]);for(inti =0; i < k -1; ++i){// Update ans appending the// substring K - 1 timesans += suff;}// Append the original stringans += s;// Returning min length string// which contain exactly k// substring of given stringreturnans;}// Driver codepublicstaticvoidmain (String[] args){intk =3;String s ="geeksforgeeks";System.out.println(findString(s, k));}}// This code is contributed by offbeatPython3
# Python3 program to implement# the above approach# KMP algorithmdefkmp(s):n=len(s)lps=[None]*nlps[0]=0i,Len=1,0while(i < n):if(s[i]==s[Len]):Len+=1lps[i]=Leni+=1else:if(Len!=0):Len=lps[Len-1]else:lps[i]=0i+=1returnlps# Function to return the required stringdeffindString(s, k):n=len(s)# Finding the longest proper prefix# which is also suffixlps=kmp(s)# ans stringans=""suff=s[0: n-lps[n-1] :1]foriinrange(k-1):# Update ans appending the# substring K - 1 timesans+=suff# Append the original stringans+=s# Returning min length string# which contain exactly k# substring of given stringreturnans# Driver codek=3s="geeksforgeeks"print(findString(s, k))# This code is contributed by divyeshrabadiya07C#
// C# program to implement// the above approachusingSystem;classGFG{// KMP algorithmstaticint[] kmp(strings){intn = s.Length;int[] lps =newint[n];lps[0] = 0;inti = 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++;}}}returnlps;}// Function to return the required stringstaticstringfindString(strings,intk){intn = s.Length;// Finding the longest proper prefix// which is also suffixint[] lps = kmp(s);// ans stringstringans ="";stringsuff = s.Substring(0,n - lps[n - 1]);for(inti = 0; i < k - 1; ++i){// Update ans appending the// substring K - 1 timesans += suff;}// Append the original stringans += s;// Returning min length string// which contain exactly k// substring of given stringreturnans;}// Driver codepublicstaticvoidMain (string[] args){intk = 3;strings ="geeksforgeeks";Console.Write(findString(s, k));}}// This code is contributed by rutvik_56Output:geeksforgeeksforgeeksforgeeks
Time Complexity: O(N*K )
Auxiliary Space: O(K* N)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes


