Given a string str, its sub-strings are formed in such a way that all the sub-strings starting with the first character of the string will occur first in the sorted order of their lengths followed by all the sub-strings starting with the second character of the string in the sorted order of their lengths and so on.
For example for the string abc, its sub-strings in the required order are a, ab, abc, b, bc and c.
Now given an integer k, the task is to find the kth sub-string in the required order.
Input: str = abc, k = 4
The required order is “a”, “ab”, “abc”, “b”, “bc” and “c”
Input: str = abc, k = 9
Only 6 sub-strings are possible.
Approach: The idea is to use binary search. An array substring will be used to store the number of sub-strings starting with ith character + substring[i – 1]. Now using binary search on the array substring, find the starting index of the required sub-string and then find the ending index for the same sub-string with end = length_of_string – (substring[start] – k).
Below is the implementation of the above approach:
- Print Kth character in sorted concatenated substrings of a string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Rearrange a string in sorted order followed by the integer sum
- Find the lexicographically smallest string which satisfies the given condition
- Find length of longest subsequence of one string which is substring of another string
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Find the count of palindromic sub-string of a string in its sorted form
- Find the longest substring with k unique characters in a given string
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find distinct characters in distinct substrings of a string
- Check if the string satisfies the given condition
- Minimum changes required to make first string substring of second string
- Queries to check if string B exists as substring in string A
- Minimum changes required such that the string satisfies the given condition
- Number of substrings of a 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.