K | Python3 implementation of the above approach ; Set to store the unique substring ; String to create each substring ; adding to set ; converting set into a list ; sorting the strings int the list into lexicographical order ; printing kth substring ; Driver Code
def kThLexString ( st , k , n ) : NEW_LINE INDENT z = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT pp = " " NEW_LINE for j in range ( i , i + k ) : NEW_LINE INDENT if ( j >= n ) : NEW_LINE INDENT break NEW_LINE DEDENT pp += s [ j ] NEW_LINE z . add ( pp ) NEW_LINE DEDENT DEDENT fin = list ( z ) NEW_LINE fin . sort ( ) NEW_LINE print ( fin [ k - 1 ] ) NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE k = 5 NEW_LINE n = len ( s ) NEW_LINE kThLexString ( s , k , n ) NEW_LINE