Lexicographically smallest string of maximum length made up of first K alphabets that does not contain any repeating substring | Java program for the above approach ; Function to find the lexicographically smallest string of the first K lower case alphabets having unique substrings ; Stores the resultant string ; Iterate through all the characters ; Inner Loop for making pairs and adding them into string ; Adding first character so that substring consisting of the last the first alphabet is present ; Print the resultant string ; Driver code
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 ) ; } }