Given two numbers N and K, the task is to find the Kth string in lexicographical order if the starting string contains (N-2) x’s first and then 2 Y’s
1 ≤ K ≤ N*(N-1)/2, N*(N-1)/2 are the number of possible permutations
Input : N = 5, K = 7
Output : YXXXY
The possible strings in lexicographical order
Input : N = 8, K = 20
Output : XYXYXXXX
Inorder to find the kth position string, we have to follow the following steps-
- we have to find the position of the leftmost occurrence of ‘Y’ by iterating over all positions from n-2 to 0.
- Now while iterating, if k<=n-i-1 then this is the required position of the leftmost occurrence of ‘Y’ and the position of rightmost occurrence is n-k so we can print the answer.
- Otherwise, let’s decrease k by n-i-1, i.e, remove all strings which have the leftmost ‘Y’ at the current position and proceed to the next position. In this way we consider all possible strings in lexicographic order.
Below is the implementation of the above approach.
- Print all the combinations of a string in lexicographical order
- Generate all numbers up to N in Lexicographical Order
- Sort the words in lexicographical order in Python
- Print all longest common sub-sequences in lexicographical order
- Print all distinct circular strings of length M in lexicographical order
- K-th lexicographical string of given length
- Lexicographical concatenation of all substrings of a string
- Lexicographical Maximum substring of string
- Find a string in lexicographic order which is in between given two strings
- Print all lexicographical greater permutations of a given string
- Largest lexicographical string with at most K consecutive elements
- Count number of substrings of a string consisting of same characters
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Length of the smallest sub-string consisting of maximum distinct characters
- Find the lexicographical next balanced bracket sequence
- Sort a string according to the order defined by another string
- Rearrange a string in sorted order followed by the integer sum
- Check if the characters of a given string are in alphabetical order
- Print words of a string in reverse order
- Check whether the vowels in a string are in alphabetical order or not
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.