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
- Reverse the substrings of the given String according to the given Array of indices
- Reverse substrings of given string according to specified array indices
- Check whether two strings are equivalent or not according to given condition
- Minimize the sum of the array according the given condition
- Count of substrings of a string containing another given string as a substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Find the Kth pair in ordered list of all possible sorted pairs of the Array
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Swap Kth node from beginning with Kth node from end in a Linked List
- Find the Kth smallest element in the sorted generated array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Check if words are sorted according to new order of alphabets
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Count pairs in Array whose product is a Kth power of any positive integer
- Find the last remaining Character in the Binary String according to the given conditions
- Rearrange a string in sorted order followed by the integer sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.