K | JavaScript 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
function kThLexString ( st , k , n ) { var z = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { var pp = " " ; for ( var j = i ; j < i + k ; j ++ ) { if ( j >= n ) break ; pp += st [ j ] ; z . add ( pp ) ; } } var fin = [ ] ; z . forEach ( element => { fin . push ( element ) ; } ) ; fin . sort ( ) ; document . write ( fin [ k - 1 ] ) ; } var s = " " ; var k = 5 ; var n = s . length ; kThLexString ( s , k , n ) ;