Given a positive integer n, the task is to find the nth string in the following infinite list of all possible strings over two symbols a and b sorted lexicographically (Dictionary).
a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb, aaaa, …
Input: n = 6
Input: n = 11
A simple approach is to generate all strings up to n and then determine the nth string. However, the approach is not suitable for large values of n.
An efficient approach is based on the fact that the number of length k strings that can be generated using 2 symbols is 2k. Based on this we can calculate the relative index from the actual index(n) with respect to the length of the string in the list. The string at nth index can then be determined easily using binary form of the relative index as the list is sorted. The following formula is used for calculation,
relative index = n + 1 – 2floor(log(n + 1))
Consider the following example:
Let n = 11 then floor(log(n + 1)) = 3.
This suggests that index n consists of a length 3 string and length 3 strings start form (23 – 1) = 7th index and 7th index contains the string “aaa”.
Therefore, relative index = 11 + 1 – 23 = 4.
This is the index relative to 7. Now, the string at index n = 11 can be simply obtained from the binary interpretation of the relative index 4.
Here 0 means a and 1 means b. The table below illustrates this:
Hence the string present at 11th index (relative index 4) is “baa”
Below is the implementation of the above approach:
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.
- Number of cells in the Nth order figure
- Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum
- Print all permutations in sorted (lexicographic) order
- Minimize Steps required to obtain Sorted Order of an Array
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Minimum characters required to be removed to sort binary string in ascending order
- Find H-Index for sorted citations using Binary Search
- Nth character in Concatenated Decimal String
- Count 1's in a sorted binary array
- Find nth Magic Number
- Program to find last two digits of Nth Fibonacci number
- Find nth Fibonacci number using Golden ratio
- Find nth number that contains the digit k or divisible by k.
- Find nth term of the Dragon Curve Sequence
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Find Nth even length palindromic number formed using digits X and Y
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Find the Nth Mosaic number
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
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.