Given a string S consisting of character from ‘a’ to ‘z’. The task is to find the length of the largest substring of S which contains a character whose frequency in the sub-string is greater than or equal to half of the length of the substring.
Note: For odd-length substring, to calculate half-length consider integer division. For Example, half of 11 is 5.
Input : S = "ababbbacbcbcca" Output : 13 Substring from index 1(0-based indexing) to index 13, "babbbacbcbcca" have b's frequency equals to 6 which is equal to half of the length of substring (13/2 = 6). Input : S = "abcde" Output : 3
Simple Approach: The idea is to find all the substring of S and for each substring find the frequency of each character and compare it with half of the length of the substring. Now, among all substrings satisfying the condition, output the length of the one having largest length.
First, observe there can be only 26 distinct characters in the string S. So, consider each character one by one for having the frequency greater than or equal to the length of the sub string.
So, to find the length of the longest possible sub string which satisfy the condition, for each character we will make the prefix array of the count of the character. For example, for S = “abacdaef”, prefix array for ‘a’ will be, say freq, [1, 1, 2, 2, 2, 3, 3, 3].
We are looking for following condition to satisfy:
freq[r] - freq[l - 1] >= (r - l)/2, where l, r are the indices.
Also, we can write,
(2 * freq[r]) - r >= (2 * freq[l - 1]) - l
So, find two arrays r and l, where r[i] = (2 * freq[i]) – i and l[i] = (2 * freq[l – 1]) – l, for 1 <= i <= length of S.
Now, for every i in r, find the lower bound in l using binary search such that the index is minimum in l, say j.
So, i – j + 1 is one of the solution, find the maximum one.
Then loop the above method for each character.
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.
- Count number of rotated strings which have more number of vowels in the first half than second half
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Check if the frequency of any character is more than half the length of the string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Find a string such that every character is lexicographically greater than its immediate next character
- Smallest subarray of size greater than K with sum greater than a given value
- Longest sub-string having frequency of each character less than equal to k
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Length of largest sub-array having primes strictly greater than non-primes
- Find the element before which all the elements are smaller than it, and after which all are greater
- Longest Even Length Substring such that Sum of First and Second Half is same
- Find words which are greater than given length k using stringstream
- Find words which are greater than given length k
- Sub-strings having exactly k characters that have ASCII value greater than p
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Smallest subarray having an element with frequency greater than that of other elements
- Largest index for each distinct character in given string with frequency K
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.