Maximum Length of Equally Frequent Character Substring
StringsAuto-graded · 26 test casesLast updated
Find the longest contiguous substring where all distinct characters appear with identical frequency.
Given a string composed of lowercase English letters, determine the length of the longest contiguous substring where each distinct character that appears in it occurs exactly the same number of times.
A substring qualifies if all characters present have equal frequency. Characters not present in the substring are not considered. For instance, a substring consisting of a single repeated character qualifies because that one character appears uniformly throughout.
Write a function that takes a string and returns the length of the longest qualifying substring.
Parameters
s(str): A string of lowercase English letters
Returns
- (int): The length of the longest substring with uniform character frequencies
Constraints
1 <= len(s) <= 1000scontains only lowercase lettersathroughz- Every single character forms a valid substring, so the result is at least
1
Behavior Notes
- A substring must be contiguous and non-empty
- Only characters actually present in the substring need to have matching counts
- If multiple substrings tie for the longest length, return that length (any tie is acceptable)
Examples
Example 1
Input: max_uniform_freq_substring('aabbc')
Output: 4
Example 2
Input: max_uniform_freq_substring('aaab')
Output: 3
Example 3
Input: max_uniform_freq_substring('xyzzyxx')
Output: 6
Hints
Hint 1
Try checking all possible substrings systematically by fixing the start position and extending the end position one character at a time
Hint 2
Track the frequency of each character incrementally as you extend the window rather than recounting from scratch
Hint 3
A window is valid when the total length equals the maximum frequency multiplied by the number of distinct characters present
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
With the constraint that the string length is at most 1000, we can examine every possible substring efficiently. The key insight is to fix the starting position and extend the ending position one character at a time while maintaining running statistics that allow us to check validity in constant time.
For each starting index, we track three pieces of information as we extend rightward:
- Character frequencies: How many times each letter appears in the current window
- Distinct character count: How many different letters are present
- Maximum frequency: The highest frequency among all present characters
A substring qualifies when every distinct character appears with the same frequency. Mathematically, if a window has d distinct characters each appearing f times, the total length is d × f. Since no character can appear more than the maximum frequency, the window qualifies if and only if:
maximum_frequency × distinct_count == window_length
This equality holds precisely when all present characters appear exactly maximum_frequency times.
Why This Works
Consider a window with length L containing d distinct letters with frequencies f₁, f₂, ..., fₐ where each fᵢ ≥ 1.
- The frequencies sum to the window length:
f₁ + f₂ + ... + fₐ = L - Each frequency satisfies:
fᵢ ≤ max_frequency - Therefore:
L ≤ max_frequency × d
Equality occurs if and only if every frequency equals the maximum, which is exactly the definition of uniform frequency. Characters absent from the window have frequency zero and are not counted in the distinct count, so they are correctly ignored.
Optimization
We start with max_length = 1 since any single character qualifies. When checking starting position i, if the remaining string length n - i cannot exceed the current best, we can terminate early since no subsequent starting position can yield a better result.
Complexity
-
Time: O(n²) where n is the string length. We examine roughly n²/2 substring pairs, each requiring O(1) work for the validity check. Resetting the frequency array for each starting position adds O(26n), which is absorbed into O(n²).
-
Space: O(1) auxiliary space for the fixed-size frequency array (26 slots). The optional precomputed character codes use O(n) space but avoid repeated ASCII calculations in the inner loop.
Implementation
def max_uniform_freq_substring(s: str) -> int:
n = len(s)
char_codes = [ord(ch) - ord('a') for ch in s]
max_length = 1
for start in range(n):
if n - start <= max_length:
break
freq = [0] * 26
num_distinct = 0
max_freq = 0
for end in range(start, n):
code = char_codes[end]
new_count = freq[code] + 1
freq[code] = new_count
if new_count == 1:
num_distinct += 1
if new_count > max_freq:
max_freq = new_count
window_len = end - start + 1
if max_freq * num_distinct == window_len and window_len > max_length:
max_length = window_len
return max_length