⚡Flashcard Drill➔
NeetCode #47LC-3442EasyArrays & Hashing
← Back to All Problems

#47 · #3442 · Maximum Difference Between Even and Odd Frequency I(奇偶频次间的最大差值 I)

📌 Problem Statement & Constraints

Given a string s, find the maximum difference between the frequency of a character appearing an even number of times and the frequency of a character appearing an odd number of times. Return max(even_freq - odd_freq), or 0 if no such pair exists. Constraints: 3 <= s.length <= 100.

💡 Core Algorithmic Approaches

  1. Compute the frequency of every character with Counter.
  2. Partition the frequencies into even ones and odd ones.
  3. If either group is empty, no valid pair exists, so return 0.
  4. Otherwise the maximum difference uses the largest even frequency and the smallest odd frequency.
  5. Note that a frequency of 0 never appears in the counter, so even frequencies are always at least 2 -- no need to filter zeros.

💻 Benchmark Python3 Implementation

class Solution:
    def maxDifference(self, s: str) -> int:
        from collections import Counter
        freq = Counter(s).values()
        even = [f for f in freq if f % 2 == 0]
        odd = [f for f in freq if f % 2 == 1]
        if not even or not odd:            # need at least one of each
            return 0
        return max(even) - min(odd)

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n + k): O(n) to count, then a constant-size scan over the at most 26 distinct characters.
💾 Space Complexity
O(k) for the counter and the two lists, with k <= 26.

⚠️ Interview Pitfalls & Follow-ups

  • Forgetting the empty-group check: if every character occurs an odd number of times, there is no even frequency and the answer is 0, not a negative number.
  • Using max(odd) - min(even): the sign is reversed; the even frequency comes first and must be the larger one.
  • Including zero frequencies: characters absent from the string are not in the counter, so zeros never enter the lists -- but if you iterate over a fixed 26-letter alphabet, they would, and 0 is even, which would corrupt the answer.
  • Assuming the answer is always positive: it can be 0, and the problem allows returning 0.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Arrays & Hashing Problems

中文速览与面试要点

奇偶频次间的最大差值 I

给你一个由小写英文字母组成的字符串 s。请你找出 s 中出现次数为 奇数 的字符的最大频次,以及出现次数为 偶数 的字符的最小频次,返回它们的差值(最大奇频 − 最小偶频)。题目保证至少有一个字符出现奇数次,且至少有一个字符出现偶数次。约束:3 ≤ s.length ≤ 100;s 仅含小写英文字母。

核心要点

  • 统计频次后分两类 :用 Counter 统计每个字符的出现次数,然后分成「奇频列表」与「偶频列表」。
  • 答案公式 : max(奇频) - min(偶频) 。注意不是「最大频次 − 最小频次」,而是 奇频的最大值 减去 偶频的最小值 。
  • 题目保证 :至少一个奇频、至少一个偶频,因此 max 与 min 不会对空序列调用,无需额外判空。
  • 本题定位 :基础计数题,考察对「按奇偶分组后分别取极值」这一读题细节的理解。

常见陷阱

  • 写成 max(cnt.values()) - min(cnt.values()) :这是把「全局最大频次」与「全局最小频次」相减,忽略了奇偶约束,是本题最常见的错误。
  • 把偶频的最小值误取成奇频的最小值 :公式是「奇频最大 − 偶频最小」,两个极值来自不同的分组。
  • 忘记题目保证非空 :虽然本题无需判空,但在通用场景下应对空列表做防御。

参考实现

from collections import Counter

class Solution:
    def maxDifference(self, s: str) -> int:
        cnt = Counter(s)
        odd = [v for v in cnt.values() if v % 2 == 1]
        even = [v for v in cnt.values() if v % 2 == 0]
        return max(odd) - min(even)