# Length of the largest substring which have character with frequency greater than or equal to half of the substring

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.

**Examples:**

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.

**Effcient Approach**:

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:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the length of the longest ` `// sub string having frequency of a character ` `// greater than half of the length of the ` `// sub string ` `int` `maxLength(string s, ` `int` `n) ` `{ ` ` ` `int` `ans = INT_MIN; ` ` ` `vector<` `int` `> A, L, R; ` ` ` `int` `freq[n + 5]; ` ` ` ` ` `// for each of the character 'a' to 'z' ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` `int` `count = 0; ` ` ` `memset` `(freq, 0, ` `sizeof` `freq); ` ` ` ` ` `// finding frequency prefix array of the ` ` ` `// character ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` `if` `(s[j] - ` `'a'` `== i) ` ` ` `count++; ` ` ` `freq[j] = count; ` ` ` `} ` ` ` ` ` `// Finding the r[] and l[] arrays. ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` `L.push_back((2 * freq[j - 1]) - j); ` ` ` `R.push_back((2 * freq[j]) - j); ` ` ` `} ` ` ` ` ` `int` `max_len = INT_MIN; ` ` ` `int` `min_val = INT_MAX; ` ` ` ` ` `// for each j from 0 to n ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` `min_val = min(min_val, L[j]); ` ` ` `A.push_back(min_val); ` ` ` ` ` `int` `l = 0, r = j; ` ` ` ` ` `// Finding the lower bound of i. ` ` ` `while` `(l <= r) { ` ` ` `int` `mid = (l + r) >> 1; ` ` ` `if` `(A[mid] <= R[j]) { ` ` ` `max_len = max(max_len, j - mid + 1); ` ` ` `r = mid - 1; ` ` ` `} ` ` ` `else` `{ ` ` ` `l = mid + 1; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// storing the maximum value of i - j + 1 ` ` ` `ans = max(ans, max_len); ` ` ` ` ` `// clearing all the vector so that it clearing ` ` ` `// be use for other character. ` ` ` `A.clear(); ` ` ` `R.clear(); ` ` ` `L.clear(); ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string s = ` `"ababbbacbcbcca"` `; ` ` ` `int` `n = s.length(); ` ` ` ` ` `cout << maxLength(s, n) << ` `'\n'` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` `import` `sys ` ` ` `# Function to return the length ` `# of the longest sub string ` `# having frequency of a character ` `# greater than half of the length ` `# of the sub string ` `def` `maxLength(s, n) : ` ` ` ` ` `ans ` `=` `-` `(sys.maxsize ` `+` `1` `); ` ` ` `A, L, R ` `=` `[], [], []; ` ` ` `freq ` `=` `[` `0` `] ` `*` `(n ` `+` `5` `); ` ` ` ` ` `# for each of the character 'a' to 'z' ` ` ` `for` `i ` `in` `range` `(` `26` `) : ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# finding frequency prefix array ` ` ` `# of the character ` ` ` `for` `j ` `in` `range` `(n) : ` ` ` `if` `(` `ord` `(s[j]) ` `-` `ord` `(` `'a'` `) ` `=` `=` `i) : ` ` ` `count ` `+` `=` `1` `; ` ` ` ` ` `freq[j] ` `=` `count; ` ` ` ` ` `# Finding the r[] and l[] arrays. ` ` ` `for` `j ` `in` `range` `(n) : ` ` ` `L.append((` `2` `*` `freq[j ` `-` `1` `]) ` `-` `j); ` ` ` `R.append((` `2` `*` `freq[j]) ` `-` `j); ` ` ` ` ` `max_len ` `=` `-` `(sys.maxsize ` `+` `1` `); ` ` ` `min_val ` `=` `sys.maxsize ; ` ` ` ` ` `# for each j from 0 to n ` ` ` `for` `j ` `in` `range` `(n) : ` ` ` `min_val ` `=` `min` `(min_val, L[j]); ` ` ` `A.append(min_val); ` ` ` ` ` `l ` `=` `0` `; r ` `=` `j; ` ` ` ` ` `# Finding the lower bound of i. ` ` ` `while` `(l <` `=` `r) : ` ` ` `mid ` `=` `(l ` `+` `r) >> ` `1` `; ` ` ` `if` `(A[mid] <` `=` `R[j]) : ` ` ` `max_len ` `=` `max` `(max_len, j ` `-` `mid ` `+` `1` `); ` ` ` `r ` `=` `mid ` `-` `1` `; ` ` ` ` ` `else` `: ` ` ` `l ` `=` `mid ` `+` `1` `; ` ` ` ` ` `# storing the maximum value of i - j + 1 ` ` ` `ans ` `=` `max` `(ans, max_len); ` ` ` ` ` `# clearing all the vector so that it can ` ` ` `# be used for other characters. ` ` ` `A.clear(); ` ` ` `R.clear(); ` ` ` `L.clear(); ` ` ` ` ` `return` `ans; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"ababbbacbcbcca"` `; ` ` ` `n ` `=` `len` `(s); ` ` ` ` ` `print` `(maxLength(s, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

13

## Recommended Posts:

- Longest Even Length Substring such that Sum of First and Second Half is same
- Check if the frequency of any character is more than half the length of the string
- Substring with highest frequency length product
- Maximum length substring with highest frequency in a string
- Minimum K such that every substring of length atleast K contains a character c
- Length of the longest substring with equal 1s and 0s
- Frequency of a substring in a string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find max length odd parity substring
- Maximum length substring having all same characters after k changes
- Length of the longest substring that do not contain any palindrome
- Length of the longest valid substring
- Lexicographically smallest and largest substring of size 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 contribute@geeksforgeeks.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.