Lexicographically smallest string of maximum length made up of first K alphabets that does not contain any repeating substring
Given a positive integer K, the task is to find lexicographically the smallest string that can be generated by using the first K lowercase alphabets such that no substring of length at least 2 is repeated in the generated string.
Examples:
Input: K = 3
Output: aabacbbcca
Explanation:
In the string “aabacbbcca”, all possible substrings of length at least 2 is repeated more than once.Input: K = 4
Output: aabacadbbcbdccdda
Approach: The given problem can be solved based on the following observations:
- If all substrings of length 2 are unique, then all substrings of length greater than 2 will also be unique.
- Hence, the maximum length string should contain all unique substrings of length 2 arrange in lexicographic order such that no 3 consecutive characters in the string are the same.
Follow the steps below to solve the problem:
- Initialize a string, say S as the empty string that stores the resultant string.
- Iterate over all the first K characters of the lowercase alphabet using the variable, say i and perform the following steps:
- Append the current character i to the string S.
- Iterate from the (i + 1)th character to the Kth character, and append the character i followed by character j to the string S.
- Add the character ‘a’ to the string S so that substring consisting of the last and the first alphabet is also present in the resultant string.
- After completing the above steps, print the string S as the resultant string.
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 the lexicographically// smallest string of the first K lower// case alphabets having unique substringsvoid generateString(int K){ // Stores the resultant string string s = ""; // Iterate through all the characters for (int i = 97; i < 97 + K; i++) { s = s + char(i); // Inner Loop for making pairs // and adding them into string for (int j = i + 1; j < 97 + K; j++) { s += char(i); s += char(j); } } // Adding first character so that // substring consisting of the last // the first alphabet is present s += char(97); // Print the resultant string cout << s;}// Driver Codeint main(){ int K = 4; generateString(K); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{ // Function to find the lexicographically// smallest string of the first K lower// case alphabets having unique substringsstatic void generateString(int K){ // Stores the resultant string String s = ""; // Iterate through all the characters for(int i = 97; i < 97 + K; i++) { s = s + (char)(i); // Inner Loop for making pairs // and adding them into string for(int j = i + 1; j < 97 + K; j++) { s += (char)(i); s += (char)(j); } } // Adding first character so that // substring consisting of the last // the first alphabet is present s += (char)(97); // Print the resultant string System.out.println(s);}// Driver codepublic static void main(String []args){ int K = 4; generateString(K);}}// This code is contributed by sanjoy_62 |
C#
// C# program for the above approachusing System;class GFG{ // Function to find the lexicographically// smallest string of the first K lower// case alphabets having unique substringsstatic void generateString(int K){ // Stores the resultant string string s = ""; // Iterate through all the characters for(int i = 97; i < 97 + K; i++) { s = s + (char)(i); // Inner Loop for making pairs // and adding them into string for(int j = i + 1; j < 97 + K; j++) { s += (char)(i); s += (char)(j); } } // Adding first character so that // substring consisting of the last // the first alphabet is present s += (char)(97); // Print the resultant string Console.Write(s);}// Driver Codepublic static void Main(){ int K = 4; generateString(K);}}// This code is contributed by ukasp |
Python3
# python 3 program for the above approach# Function to find the lexicographically# smallest string of the first K lower# case alphabets having unique substringsdef generateString(K): # Stores the resultant string s = "" # Iterate through all the characters for i in range(97,97 + K,1): s = s + chr(i); # Inner Loop for making pairs # and adding them into string for j in range(i + 1,97 + K,1): s += chr(i) s += chr(j) # Adding first character so that # substring consisting of the last # the first alphabet is present s += chr(97) # Print the resultant string print(s)# Driver Codeif __name__ == '__main__': K = 4 generateString(K) # This code is contributed by SURENDRA_GANGWAR. |
Javascript
<script>// JavaScript program for the above approach // Function to find the lexicographically// smallest string of the first K lower// case alphabets having unique substringsfunction generateString(K){ // Stores the resultant string var s = ""; // Iterate through all the characters for(var i = 97; i < 97 + K; i++) { s = s + String.fromCharCode(i); // Inner Loop for making pairs // and adding them into string for(var j = i + 1; j < 97 + K; j++) { s += String.fromCharCode(i); s += String.fromCharCode(j); } } // Adding first character so that // substring consisting of the last // the first alphabet is present s += String.fromCharCode(97); // Print the resultant string document.write(s);}// Driver codevar K = 4;generateString(K);// This code is contributed by 29AjayKumar</script> |
aabacadbbcbdccdda
Time Complexity: O(K2)
Auxiliary Space: O(K2)
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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



