You are given a string s consisting of lowercase English letters.
Your task is to find the maximum difference diff = freq(a1) - freq(a2) between the frequency of characters a1 and a2 in the string such that:
a1 has an odd frequency in the string.a2 has an even frequency in the string.Return this maximum difference.
Example 1:
Input: s = "aaaaabbc"
Output: 3
Explanation:
'a' has an odd frequency of 5, and 'b' has an even frequency of 2.5 - 2 = 3.Example 2:
Input: s = "abcabcab"
Output: 1
Explanation:
'a' has an odd frequency of 3, and 'c' has an even frequency of 2.3 - 2 = 1.
Constraints:
3 <= s.length <= 100s consists only of lowercase English letters.s contains at least one character with an odd frequency and one with an even frequency.Problem Overview: You are given a string and need to compare character frequencies. The goal is to choose one character with an odd frequency and another with an even frequency, then maximize the difference oddFreq - evenFreq. If no valid pair exists, the result is typically -1.
Approach 1: Brute Force Frequency Comparison (O(n + k^2) time, O(k) space)
Start by computing the frequency of each character in the string using a map or array. After collecting all counts, separate them into two groups: characters with odd frequencies and characters with even frequencies. Compare every odd frequency with every even frequency and track the maximum difference. The complexity comes from pairwise comparisons across the two groups. This works because the number of unique characters k is small, but it is still unnecessary work when the optimal values can be derived directly.
Approach 2: Counting + Greedy Extremes (O(n + k) time, O(k) space)
The optimal approach relies on a simple observation: the maximum value of oddFreq - evenFreq will always come from the largest odd frequency and the smallest even frequency. First count character frequencies using a hash map or fixed-size array (typical for lowercase strings). Then iterate through the counts once to track two values: maxOdd and minEven. If both exist, compute maxOdd - minEven. If either group is missing, no valid pair can be formed.
This method reduces the problem to a single counting pass followed by a scan over the frequency values. The algorithm uses constant extra work per character and avoids any nested comparisons. Counting problems like this commonly appear in string interviews and are best handled with structures from hash tables or simple arrays.
The approach also highlights a common interview pattern: reduce a comparison problem into tracking extreme values during iteration. Instead of storing and comparing every candidate pair, maintain the best odd and even candidates while scanning.
Recommended for interviews: The counting approach is the expected solution. Interviewers want to see you quickly build a frequency map (a standard string technique) and reason about extremes rather than brute-force comparisons. Mentioning the brute-force idea first shows understanding, but implementing the counting optimization demonstrates stronger problem-solving skills.
We can use a hash table or an array cnt to record the occurrences of each character in the string s. Then, we traverse cnt to find the maximum frequency a of characters that appear an odd number of times and the minimum frequency b of characters that appear an even number of times. Finally, we return a - b.
The time complexity is O(n), where n is the length of the string s. The space complexity is O(|\Sigma|), where \Sigma is the character set. In this problem, |\Sigma| = 26.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Frequency Comparison | O(n + k^2) | O(k) | Useful for understanding the problem before optimizing |
| Counting with Extreme Tracking | O(n + k) | O(k) | Best general solution for strings using frequency counting |
Maximum Difference Between Even and Odd Frequency I | Easy | Leetcode 3442 | codestorywithMIK • codestorywithMIK • 4,450 views views
Watch 9 more video solutions →Practice Maximum Difference Between Even and Odd Frequency I with our built-in code editor and test cases.
Practice on FleetCode