Find the Most Frequent Character Difference
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests whether a candidate can use hash counting to find the pair of distinct characters whose absolute frequency difference is largest in a given string.
Find the Most Frequent Character Difference
You are given a non-empty string s made up of lowercase English letters.
Define the frequency difference of an ordered pair of distinct characters (a, b) (where a != b and both appear in s) as:
freq(a) - freq(b)
where freq(c) is the number of times character c appears in s.
Your task is to find the maximum frequency difference across all valid ordered pairs (a, b), and return the character a that achieves it.
- If multiple characters
acould achieve the same maximum difference (tied most-frequent characters paired against the same least-frequent character), return the one that comes first alphabetically. - It is guaranteed that
scontains at least 2 distinct characters.
Constraints
1 <= len(s) <= 2000sconsists only of lowercase English letters ('a'–'z').scontains at least 2 distinct characters.
Examples
Example 1
Input: most_frequent_char_diff('aabbbbcc')
Output: 'b'
Example 2
Input: most_frequent_char_diff('abcabc')
Output: 'a'
Example 3
Input: most_frequent_char_diff('aaabbc')
Output: 'a'
Hints
Hint 1
Count the frequency of every character in the string using a dictionary or Counter.
Hint 2
The maximum frequency difference is simply max_freq - min_freq, where max_freq and min_freq are the highest and lowest frequencies among all characters that appear in s.
Hint 3
If multiple characters share the maximum frequency, return the alphabetically smallest one (standard min() over a list of tied characters).
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The maximum value of freq(a) - freq(b) over all distinct character pairs (a, b) is achieved when freq(a) is as large as possible and freq(b) is as small as possible. Since we are free to choose any valid b != a, the best b is always the character with the global minimum frequency. Therefore:
max_diff = max_freq - min_freq
Any character a with freq(a) = max_freq achieves this maximum. If multiple characters tie at max_freq, the problem asks us to return the alphabetically smallest one.
Algorithm
- Count character frequencies with a hash map (
collections.Counter). - Find
min_freq = min(freq.values()). - Compute the difference for every character:
freq(c) - min_freq. - Find the maximum difference value
max_diff. - Collect all characters achieving
max_diff, sort them, return the first.
Why It Is Correct
- The pair
(a, b)that maximisesfreq(a) - freq(b)must use the globally least-frequent character asb(since loweringfreq(b)only increases the difference). - Correspondingly,
amust be the globally most-frequent character. - Alphabetical tie-breaking is deterministic.
Complexity
| Value | |
|---|---|
| Time | O(n + k log k) where n = len(s), k ≤ 26 |
| Space | O(k) ≤ O(26) = O(1) |
The dominant cost is a single pass to build the frequency table; everything else operates on at most 26 entries.
from collections import Counter
def most_frequent_char_diff(s: str) -> str:
freq = Counter(s)
min_freq = min(freq.values())
max_diff = max(v - min_freq for v in freq.values())
# Collect all characters achieving the max difference, return alphabetically smallest
candidates = sorted(c for c, v in freq.items() if v - min_freq == max_diff)
return candidates[0]