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


In this problem, we need to generate a lexicographically smallest string using the first K characters of alphabets so that it doesn’t contain any repeated substring. We can generate a string so that all substrings of length 2 are unique. So, if all substrings of length 2 are unique, all substrings of length 3 or more are also unique.

Problem statement – We have given a positive integer K. We need to generate a new string using the first K alphabets so that generated string can’t contain any repeated substring of length 2 or more and is lexicographically smallest.

Sample examples

Input– K = 1

Output– ‘aa’

Explanation– We can generate ‘aa’ using only ’a’ so that the resultant string contains all unique substrings of length 2 or more.

Input– K = 2

Output– ‘aabba’

Explanation– The ‘aabba’ is the lexicographically smallest string that we can generate using ‘a’ and ‘b’ only.

Input– K = 4

Output– ‘aabacadbbcbdccdda’

Explanation– The ‘aabacadbbcbdccdda’ is the smallest string we can generate using the first 4 alphabetical characters.

Approach 1

This approach will use two nested loops to generate a new string containing unique substrings of length 2.

Algorithm

  • Initialize the ‘str’ variable with the empty string.

  • Iterate in the range 97 to 97 + k, where 97 is the ASCII value of the ‘a’.

  • Append the current character in the ‘str’.

  • Iterate in the range I to 97 + k.

  • Convert i and j to character value and append to the str.

  • When iteration of both nested loops completes, append ‘a’ at the end of the str.

  • Return str, which is a newly generated string using the first K alphabets.

Example

#include <bits/stdc++.h>
using namespace std;

// function to return the lexicographically smallest string of length K
string getTheString(int K){
   // to store the resultant string
   string str = "";
   // Iterating over the range [97, 97 + K]
   for (int i = 97; i < 97 + K; i++){
      // appending the ith character to the string
      str = str + char(i);
      // Create a substring of length 2 consisting of the ith character and the jth character
      for (int j = i + 1; j < 97 + K; j++){
          // appending i and j to str
          str += char(i);
          str += char(j);
      }
   }
   // appending the last character to the string
   str += char(97);
   // return the resultant string
   return str;
}
int main(){
   int K = 2;
   cout << "The lexicographically smallest string is " << getTheString(K);
   return 0;
}

Output

The lexicographically smallest string is aabba

Time complexity – O(K * K) as both loop traverse the first K characters.

Space complexity – O(K * K) as we store the newly generated string to the ‘str’ variable.

In the above code, we generate all the unique substrings of length 2 using the first k character. We need to create unique pairs of characters to create a unique substring of length 2, and that logic is followed in the solution approach.

Updated on: 18-Aug-2023

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements