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:
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 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 length of longest subsequence of one string which is substring of another string
- Find the lexicographically smallest string which satisfies the given condition
- Queries to find frequencies of a string within specified substrings
- Queries to find the count of vowels in the substrings of the given string
- Find the count of palindromic sub-string of a string in its sorted form
- Find the String having each substring with exactly K distinct characters
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- 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
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Queries to check if string B exists as substring in string A
- Minimum changes required to make first string substring of second string
- Check if given Binary string follows then given condition or not
- Check if the string satisfies the given condition
- Minimum changes required such that the string satisfies the given condition
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.