Given a string which contains lower alphabetic characters, we need to find out such a substring of this string whose product of length and frequency in string is maximum among all possible choices of substrings.
Input : String str = “abddab” Output : 6 All unique substring with product of their frequency and length are, Val["a"] = 2 * 1 = 2 Val["ab"] = 2 * 2 = 4 Val["abd"] = 1 * 3 = 3 Val["abdd"] = 1 * 4 = 4 Val["abdda"] = 1 * 5 = 5 Val["abddab"] = 1 * 6 = 6 Val["b"] = 2 * 1 = 2 Val["bd"] = 1 * 2 = 2 Val["bdd"] = 1 * 3 = 3 Val["bdda"] = 1 * 4 = 4 Val["bddab"] = 1 * 5 = 5 Val["d"] = 2 * 1 = 2 Val["da"] = 1 * 2 = 2 Val["dab"] = 1 * 3 = 3 Val["dd"] = 1 * 2 = 2 Val["dda"] = 1 * 3 = 3 Val["ddab"] = 1 * 4 = 4 Input : String str = “zzzzzz” Output : 12 In above string maximum value 12 can be obtained with substring “zzzz”
A simple solution is to consider all substrings one by one. For every substring, count number of occurrences of it in whole string.
An efficient solution to solve this problem by first constructing longest common prefix array, now suppose value of lcp[i] is K then we can say that i-th and (i+1)-th suffix has K length prefix in common i.e. there is a substring of length K which is repeating twice. In the same way, let three consecutive values of lcp are (K, K-2, K+1) then we can say that there is a substring of length (K-2) which is repeating three times in the string.
Now after above observation, we can see that our result will be such a range of lcp array whose smallest element times number of elements in the range is maximum because range will correspond to the frequency of string and smallest element of range will correspond to length of repeating string now this reformed problem can be solved similar to largest rectangle in histogram problem.
In below code lcp array is constructed by Kasai’s algorithm.
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 email@example.com. 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.
- Maximum length substring with highest frequency in a string
- Frequency of a substring in a string
- Length of the longest valid substring
- Maximum length substring having all same characters after k changes
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring with equal 1s and 0s
- Minimum K such that every substring of length atleast K contains a character c
- Length of the longest substring without repeating characters
- Convert to a string that is repetition of a substring of k length
- Encrypt string with product of number of vowels and consonants in substring of size k
- Find length of longest subsequence of one string which is substring of another string
- 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
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Sort elements by frequency | Set 5 (using Java Map)