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.
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.
- 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
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Substring of length K having maximum frequency in the given string
- Minimum length of substring whose rotation generates a palindromic substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Frequency of a substring in a string using pthread
- Frequency of a substring in a string
- Frequency of a substring in a string | Set 2
- 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
- Encrypt string with product of number of vowels and consonants in substring of size k
- Find the Substring with maximum product
- Check if the frequency of any character is more than half the length of the string
- Find the length of the longest subsequence with first K alphabets having same frequency
- Length of the longest substring without repeating characters
- Longest Even Length Substring such that Sum of First and Second Half is same
Improved By : Akanksha_Rai