Given a string of lower alphabetic characters, find K-th character in a string formed by substrings (of given string) when concatenated in sorted form.
Input : str = “banana” K = 10 Output : n All substring in sorted form are, "a", "an", "ana", "anan", "anana", "b", "ba", "ban", "bana", "banan", "banana", "n", "na", "nan", "nana" Concatenated string = “aananaanana nanabbabanbanabananbananannanannana” We can see a 10th character in the above concatenated string is ‘n’ which is our final answer.
A simple solution is to generate all substrings of a given string and store them in an array. Once substrings are generated, sort them and concatenate after dorting. Finally print K-th character in the concatenated string.
An efficient solution is based om counting distinct substring of a string using suffix array. Same method is used in solving this problem also. After getting suffix array and lcp array, we loop over all lcp values and for each such value, we calculate characters to skip. We keep subtracting these many characters from our K, when character to skip becomes more than K, we stop and loop over substrings corresponding to current lcp[i], in which we loop from lcp[i] till the maximum length of string and then print the Kth character.
This article is contributed by Utkarsh Trivedi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Nth character in Concatenated Decimal String
- Print the final string when minimum value strings get concatenated in every operation
- Count substrings that starts with character X and ends with character Y
- Concatenated string with uncommon characters of two strings
- Concatenated string with uncommon characters in Python
- Maximum Consecutive Zeroes in Concatenated Binary String
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Nearest smaller character to a character K from a Sorted Array
- Program to find the kth character after decrypting a string
- Queries for rotation and Kth character of the given string in constant time
- Kth most frequent Character in a given String
- Kth character from the Nth string obtained by the given operations
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Number of substrings with count of each character as k
- Count substrings with each character occurring at most k times
- Count of substrings which contains a given character K times