Maximum Difference Between Even and Odd Frequency I - Video Solutions
Maximum Difference Between Even and Odd Frequency I | Easy | Leetcode 3442 | codestorywithMIK
Maximum Difference Between Even and Odd Frequency I - Video Solution
Watch 10 video solutions for Maximum Difference Between Even and Odd Frequency I, a easy level problem involving Hash Table, String, Counting. This walkthrough by codestorywithMIK has 4,556 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
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:
a1has an odd frequency in the string.a2has an even frequency in the string.
Return this maximum difference.
Example 1:
Input: s = "aaaaabbc"
Output: 3
Explanation:
- The character
'a'has an odd frequency of5, and'b'has an even frequency of2. - The maximum difference is
5 - 2 = 3.
Example 2:
Input: s = "abcabcab"
Output: 1
Explanation:
- The character
'a'has an odd frequency of3, and'c'has an even frequency of 2. - The maximum difference is
3 - 2 = 1.
Constraints:
3 <= s.length <= 100sconsists only of lowercase English letters.scontains at least one character with an odd frequency and one with an even frequency.
Approach Overview
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.
Complexity Analysis
| 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 |