Given two integers N and K, the task is to find lexicographically Kth string of length N. If the number of possible strings of length N is less than K, print -1.
Examples:
Input: N = 3, K = 10
Output: “aaj”
Explanation: The 10th string in the lexicographical order starting from “aaa” is “aaj”.Input: N = 2, K = 1000
Output: -1
Explanation: A total of 26*26 = 676 strings of length 2 are possible. So the output will be -1.
Approach:
Below is the implementation of the above approach:
C++
// C++ program to find the K-th // lexicographical string of length N #include <bits/stdc++.h> using namespace std; string find_kth_String_of_n(int n, int k) { // Initialize the array to store // the base 26 representation of // K with all zeroes, that is, // the initial String consists // of N a's int d[n] = {0}; // Start filling all the // N slots for the base 26 // representation of K for(int i = n - 1; i > -1; i--) { // Store the remainder d[i] = k % 26; // Reduce K k /= 26; } // If K is greater than the // possible number of strings // that can be represented by // a string of length N if (k > 0) return "-1"; string s = ""; // Store the Kth lexicographical // string for(int i = 0; i < n; i++) s += (d[i] + ('a')); return s; } // Driver Code int main() { int n = 3; int k = 10; // Reducing k value by 1 because // our stored value starts from 0 k -= 1; cout << find_kth_String_of_n(n, k); return 0; } // This code is contributed by 29AjayKumar |
Java
// Java program to find the // K-th lexicographical String // of length N class GFG{ static String find_kth_String_of_n(int n, int k) { // Initialize the array to // store the base 26 // representation of K // with all zeroes, that is, // the initial String consists // of N a's int[] d = new int[n]; // Start filling all the // N slots for the base 26 // representation of K for (int i = n - 1; i > -1; i--) { // Store the remainder d[i] = k % 26; // Reduce K k /= 26; } // If K is greater than the // possible number of Strings // that can be represented by // a String of length N if (k > 0) return "-1"; String s = ""; // Store the Kth lexicographical // String for (int i = 0; i < n; i++) s += (char)(d[i] + ('a')); return s; } public static void main(String[] args) { int n = 3; int k = 10; // Reducing k value by 1 because // our stored value starts from 0 k -= 1; System.out.println(find_kth_String_of_n(n, k)); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python program to find the # K-th lexicographical string # of length N def find_kth_string_of_n(n, k): # Initialize the array to # store the base 26 # representation of K # with all zeroes, that is, # the initial string consists # of N a's d =[0 for i in range(n)] # Start filling all the # N slots for the base 26 # representation of K for i in range(n - 1, -1, -1): # Store the remainder d[i]= k % 26 # Reduce K k//= 26 # If K is greater than the # possible number of strings # that can be represented by # a string of length N if k>0: return -1 s ="" # Store the Kth lexicographical # string for i in range(n): s+= chr(d[i]+ord('a')) return s n = 3k = 10# Reducing k value by 1 because # our stored value starts from 0 k-= 1 print(find_kth_string_of_n(n, k)) |
C#
// C# program to find the // K-th lexicographical String // of length N using System; class GFG{ static String find_kth_String_of_n(int n, int k) { // Initialize the array to // store the base 26 // representation of K // with all zeroes, that is, // the initial String consists // of N a's int[] d = new int[n]; // Start filling all the // N slots for the base 26 // representation of K for (int i = n - 1; i > -1; i--) { // Store the remainder d[i] = k % 26; // Reduce K k /= 26; } // If K is greater than the // possible number of Strings // that can be represented by // a String of length N if (k > 0) return "-1"; string s = ""; // Store the Kth lexicographical // String for (int i = 0; i < n; i++) s += (char)(d[i] + ('a')); return s; } // Driver Code public static void Main() { int n = 3; int k = 10; // Reducing k value by 1 because // our stored value starts from 0 k -= 1; Console.Write(find_kth_String_of_n(n, k)); } } // This code is contributed by Code_Mech |
aaj
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all distinct circular strings of length M in lexicographical order
- Lexicographical Maximum substring of string
- Lexicographical concatenation of all substrings of a string
- Largest lexicographical string with at most K consecutive elements
- Print all lexicographical greater permutations of a given string
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Print all the combinations of a string in lexicographical order
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Find length of longest subsequence of one string which is substring of another string
- Minimize length of prefix of string S containing all characters of another string T
- Minimum length of string having all permutation of given string.
- Check if a string contains a palindromic sub-string of even length
- Length Of Last Word in a String
- All possible strings of any length that can be formed from a given string
- Program for length of a string using recursion
- Length of Longest sub-string that can be removed
- Print string of odd length in 'X' format
- Lexicographically smallest string of length N and sum K
- Count of sub-strings of length n possible from the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

