# Lexicographically smallest string of maximum length made up of first K alphabets that does not contain any repeating substring

Last Updated : 03 Aug, 2021

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

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 ` `using` `namespace` `std;`   `// Function to find the lexicographically` `// smallest string of the first K lower` `// case alphabets having unique substrings` `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` `    ``cout << s;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `K = 4;` `    ``generateString(K);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{` `    `  `// Function to find the lexicographically` `// smallest string of the first K lower` `// case alphabets having unique substrings` `static` `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 code` `public` `static` `void` `main(String []args)` `{` `    ``int` `K = ``4``;` `    `  `    ``generateString(K);` `}` `}`   `// This code is contributed by sanjoy_62`

## C#

 `// C# program for the above approach` `using` `System;`   `class` `GFG{` `    `  `// Function to find the lexicographically` `// smallest string of the first K lower` `// case alphabets having unique substrings` `static` `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 Code` `public` `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 substrings` `def` `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 Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``K ``=` `4` `    ``generateString(K)`   `    ``# This code is contributed by SURENDRA_GANGWAR.`

## Javascript

 ``

Output:

`aabacadbbcbdccdda`

Time Complexity: O(K2)
Auxiliary Space: O(K2)

Previous
Next