Given a string S. The task is to print the K-th lexicographically the smallest one among the different substrings of s.
A substring of s is a string obtained by taking out a non-empty contiguous part in s.
For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z, and an empty string are not. Also, we say that substrings are different when they are different as strings.
Input: str = “aba”, k = 4
All unique substrings are a, ab, aba, b, ba.
Thus the 4th lexicographically smallest substring is b.
Input: str = “geeksforgeeks”, k = 5
Approach: For an arbitrary string t, each of its proper suffixes is lexicographically smaller than t, and the lexicographic rank of t is at least |t|. Thus, the length of the answer is at most K.
Generate all substrings of s whose lengths are at most K. Sort them, unique them, and print the K-th one, where N = |S|.
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.
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically Kth smallest way to reach given coordinate from origin
- Lexicographically smallest and largest substring of size k
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest K-length substring containing maximum number of vowels
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Find the longest substring with k unique characters in a given string
- Check if string can be made lexicographically smaller by reversing any substring
- Length of smallest substring of a given string which contains another string as subsequence
- Lexicographically smallest permutation of a string with given subsequences
- Find the lexicographically smallest string which satisfies the given condition
- Lexicographically smallest string which differs from given strings at exactly K indices
- Lexicographically smallest subsequence possible by removing a character from given string
- Lexicographically smallest string possible by inserting given character
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Smallest String consisting of a String S exactly K times as a Substring
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.