K | Javascript program to find the K - th lexicographical String of length N ; Initialize the array to store the base 26 representation of K with all zeroes , that is , the initial String consists of N a 's ; Start filling all the N slots for the base 26 representation of K ; Store the remainder ; Reduce K ; If K is greater than the possible number of Strings that can be represented by a String of length N ; Store the Kth lexicographical String ; Driver Code ; Reducing k value by 1 because our stored value starts from 0
function find_kth_String_of_n ( n , k ) { let d = new Array ( n ) ; d . fill ( 0 ) ; for ( let i = n - 1 ; i > - 1 ; i -- ) { d [ i ] = k % 26 ; k = parseInt ( k / 26 , 10 ) ; } if ( k > 0 ) return " " ; let s = " " ; for ( let i = 0 ; i < n ; i ++ ) s += String . fromCharCode ( d [ i ] + ( ' ' ) . charCodeAt ( ) ) ; return s ; } let n = 3 ; let k = 10 ; k -= 1 ; document . write ( find_kth_String_of_n ( n , k ) ) ;