K | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string find_kth_String_of_n ( int n , int k ) { int d [ n ] = { 0 } ; for ( int i = n - 1 ; i > -1 ; i -- ) { d [ i ] = k % 26 ; k /= 26 ; } if ( k > 0 ) return " - 1" ; string s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( d [ i ] + ( ' a ' ) ) ; return s ; } int main ( ) { int n = 3 ; int k = 10 ; k -= 1 ; cout << find_kth_String_of_n ( n , k ) ; return 0 ; }