K | Python 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
def find_kth_string_of_n ( n , k ) : NEW_LINE INDENT d = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT d [ i ] = k % 26 NEW_LINE k //= 26 NEW_LINE DEDENT if k > 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT s = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += chr ( d [ i ] + ord ( ' a ' ) ) NEW_LINE DEDENT return s NEW_LINE DEDENT n = 3 NEW_LINE k = 10 NEW_LINE k -= 1 NEW_LINE print ( find_kth_string_of_n ( n , k ) ) NEW_LINE