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
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Frequency of a substring in a string
- Frequency of a substring in a string using pthread
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Check if the frequency of any character is more than half the length of the string
- Length of the longest valid substring
- Length of the longest substring that do not contain any palindrome
- Longest Even Length Substring such that Sum of First and Second Half is same
- Find max length odd parity substring
- Length of the longest substring with equal 1s and 0s
- Maximum length substring having all same characters after k changes
- Minimum K such that every substring of length atleast K contains a character c
- Minimum length substring with exactly K distinct characters
- Convert to a string that is repetition of a substring of k length
Improved By : Akanksha_Rai