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
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++
#include <bits/stdc++.h>
using namespace std;
void generateString( int K)
{
string s = "" ;
for ( int i = 97; i < 97 + K; i++) {
s = s + char (i);
for ( int j = i + 1;
j < 97 + K; j++) {
s += char (i);
s += char (j);
}
}
s += char (97);
cout << s;
}
int main()
{
int K = 4;
generateString(K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void generateString( int K)
{
String s = "" ;
for ( int i = 97 ; i < 97 + K; i++)
{
s = s + ( char )(i);
for ( int j = i + 1 ; j < 97 + K; j++)
{
s += ( char )(i);
s += ( char )(j);
}
}
s += ( char )( 97 );
System.out.println(s);
}
public static void main(String []args)
{
int K = 4 ;
generateString(K);
}
}
|
C#
using System;
class GFG{
static void generateString( int K)
{
string s = "" ;
for ( int i = 97; i < 97 + K; i++)
{
s = s + ( char )(i);
for ( int j = i + 1; j < 97 + K; j++)
{
s += ( char )(i);
s += ( char )(j);
}
}
s += ( char )(97);
Console.Write(s);
}
public static void Main()
{
int K = 4;
generateString(K);
}
}
|
Python3
def generateString(K):
s = ""
for i in range ( 97 , 97 + K, 1 ):
s = s + chr (i);
for j in range (i + 1 , 97 + K, 1 ):
s + = chr (i)
s + = chr (j)
s + = chr ( 97 )
print (s)
if __name__ = = '__main__' :
K = 4
generateString(K)
|
Javascript
<script>
function generateString(K)
{
var s = "" ;
for ( var i = 97; i < 97 + K; i++)
{
s = s + String.fromCharCode(i);
for ( var j = i + 1; j < 97 + K; j++)
{
s += String.fromCharCode(i);
s += String.fromCharCode(j);
}
}
s += String.fromCharCode(97);
document.write(s);
}
var K = 4;
generateString(K);
</script>
|
Output:
aabacadbbcbdccdda
Time Complexity: O(K2)
Auxiliary Space: O(K2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...