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.
Input: N = 3, K = 10
Explanation: The 10th string in the lexicographical order starting from “aaa” is “aaj”.
Input: N = 2, K = 1000
Explanation: A total of 26*26 = 676 strings of length 2 are possible. So the output will be -1.
Below is the implementation of the above approach:
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.
- Print all distinct circular strings of length M in lexicographical order
- Print all lexicographical greater permutations of a given string
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Print all the combinations of a string in lexicographical order
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Largest lexicographical string with at most K consecutive elements
- Lexicographical concatenation of all substrings of a string
- Lexicographical Maximum substring of string
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Difference between lexicographical ranks of two given permutations
- Print all longest common sub-sequences in lexicographical order
- Sort the words in lexicographical order in Python
- Find the lexicographical next balanced bracket sequence
- Lexicographical smallest alternate Array
- Lexicographical ordering using Heap Sort
- Lexicographical smallest number after at most K consecutive swaps
- Generate all numbers up to N in Lexicographical Order
- Print all numbers up to N in words in lexicographical order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.