Highest Frequency Letter with Alphabetical Tie-Breaking
StringsAuto-graded · 22 test casesLast updated
Tests efficient character frequency counting and consistent tie-breaking using fixed-size auxiliary space.
Problem Statement
You are given a string s made up entirely of lowercase English letters. Your task is to identify which letter occurs the greatest number of times in the string. When multiple letters share the highest occurrence count, return the one that appears earliest in the alphabet.
Your implementation must handle strings with up to ten million characters efficiently.
Function Signature
def highest_frequency_letter(s: str) -> str:
pass
Input & Output Format
- Input: A non-empty string
scontaining only lowercase English letters ('a'to'z'). - Output: A single character (string of length 1) representing the letter with the highest frequency. If multiple letters tie for the highest count, return the lexicographically smallest one.
Constraints
1 <= len(s) <= 10^7sconsists exclusively of lowercase English letters ('a'through'z')- Your solution should run in
O(n)time wherenis the length ofs - Aim for
O(1)auxiliary space (beyond the input itself)
Edge Cases to Consider
- Strings where one letter dominates completely
- Strings where all letters appear with equal frequency
- Strings where every character is unique
- Very long inputs approaching the maximum constraint
- Single-character strings
Examples
Example 1
Input: highest_frequency_letter('abacaba')
Output: 'a'
Example 2
Input: highest_frequency_letter('dcbaabcd')
Output: 'a'
Hints
Hint 1
The English alphabet has exactly 26 letters—consider using a fixed-size structure rather than a dynamic hash map.
Hint 2
Walk through the frequency array in alphabetical order and only update your answer when you find a strictly greater count.
Hint 3
A single pass to count and a second pass over 26 slots to find the maximum is O(n + 26) = O(n) total.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Since the input is restricted to lowercase English letters, we can exploit the fact that there are exactly 26 possible characters. Rather than using a hash map that grows with the number of distinct characters, we use a fixed array of 26 integers where index 0 corresponds to 'a', index 1 to 'b', and so on through index 25 for 'z'.
Step 1: Count frequencies
Traverse the string once. For each character, compute its index as ord(ch) - ord('a') and increment the corresponding counter in our array. This takes O(n) time.
Step 2: Find the maximum with tie-breaking
Scan through indices 0 to 25 in order. Track the index with the highest count seen so far. Crucially, only update this tracked index when we encounter a count that is strictly greater than the current best. Because we scan in ascending order (alphabetical order) and only replace on strict inequality, ties automatically resolve in favor of the earlier letter.
Why it works
- Counting is exact: each character maps to exactly one slot.
- The selection pass maintains the invariant that after examining indices 0 through i,
bestholds the smallest index with the maximum count in that range. - Letters that don't appear in the string have count 0 and cannot become
bestunless no letter appears at all—but the input is guaranteed non-empty, so at least one letter has count ≥ 1.
Complexity
- Time: O(n) for the counting pass + O(26) for the selection pass = O(n) overall
- Space: O(1) auxiliary space—the 26-element array is constant size, independent of input length
Edge cases handled
- Single character strings: the counting phase records one occurrence, the scan identifies the correct letter
- All letters tied: strict
>ensures we keep the first (alphabetically smallest) index - All distinct letters: each present letter has count 1; we return the alphabetically first one present
- Very large inputs: no per-character allocations, just index arithmetic and array updates
def highest_frequency_letter(s: str) -> str:
counts = [0] * 26
for ch in s:
counts[ord(ch) - ord('a')] += 1
best = 0
for i in range(1, 26):
if counts[i] > counts[best]:
best = i
return chr(ord('a') + best)