Maximum Difference Between Even and Odd Frequency I (LeetCode 3442): Solution and Explanation

Easy
Hash Table
String
Counting

Pick one letter that appears an odd number of times and one that appears an even number of times, and return the largest possible value of the first count minus the second.

Intuition

Only letter counts matter, so start by counting the 26 letters.

The difference grows when the odd count is large and the even count is small. The two picks are independent, so take the largest odd count and the smallest even count separately.

Letters that do not appear have count 0. Zero is even, but those letters are not in the string, so they cannot be chosen as a2.

Approach: Largest odd count minus smallest even count

  1. Count each letter of s.
  2. Set max_odd = 0 and min_even = len(s).
  3. For every count c: if c is odd, raise max_odd; else if c > 0, lower min_even.
  4. Return max_odd - min_even.

Step-by-step example

s = "aaaaabbc"
  1. Counts: a = 5, b = 2, c = 1.
  2. Odd counts are 5 ('a') and 1 ('c'), so max_odd = 5.
  3. The only even count is 2 ('b'), so min_even = 2.
  4. Return 5 - 2 = 3.

Code: Python, Java, C++

1class Solution:
2    def maxDifference(self, s: str) -> int:
3        cnt = [0] * 26
4        for ch in s:
5            cnt[ord(ch) - ord('a')] += 1
6        max_odd, min_even = 0, len(s)
7        for c in cnt:
8            if c % 2 == 1:
9                max_odd = max(max_odd, c)
10            elif c > 0:
11                min_even = min(min_even, c)
12        return max_odd - min_even
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass counts the letters, then a fixed 26-step loop finds both extremes.

Space

O(1)

The count table has 26 entries regardless of the input length.

Other approaches

Try every pair (time O(n + 26^2), space O(1))

Check every odd-count letter against every even-count letter and keep the best difference. With at most 26 letters this is still fast, but it hides the fact that the two choices are independent.

Counter with filters (time O(n), space O(1))

Build a Counter, then compute max(c for c in values if c % 2) and min(c for c in values if c % 2 == 0). The same idea in two lines.

Common mistakes

  • Including letters with count 0 as even. They are not in the string, and using them would wrongly subtract 0.
  • Taking the largest even count instead of the smallest. Subtracting a bigger number makes the difference smaller.
  • Assuming the answer is positive. For "aaaab" the only odd count is 1 and the only even count is 4, so the answer is -3.

Frequently asked questions

Can the answer be negative?

Yes. If every odd count is smaller than every even count, the best you can do is still negative, as in "aaaab" which gives 1 - 4 = -3.

Why start min_even at len(s)?

No letter can appear more than len(s) times, so any real even count will replace it. The constraints guarantee at least one even count exists.

Does the same letter ever count for both sides?

No. A count is either odd or even, so each letter belongs to exactly one side.