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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all distinct circular strings of length M in lexicographical order
- Lexicographical concatenation of all substrings of a string
- Lexicographical Maximum substring of 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
- Largest lexicographical string with at most K consecutive elements
- Print all lexicographical greater permutations of a given string
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find length of longest subsequence of one string which is substring of another string
- 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
- Lexicographically smallest string of length N and sum K
- Count subsequence of length three in a given string
- Length of Longest sub-string that can be removed
- Program for length of a string using recursion
- Print string of odd length in 'X' format
- All possible strings of any length that can be formed from a given string
- 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 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.