Hey tech enthusiasts! 👋
Today, we’re diving into a fun and insightful string frequency problem — LeetCode 3442: Maximum Difference Between Even and Odd Frequency I.
This one’s all about counting how often characters appear and performing a bit of clever subtraction. Let’s break it down and solve it cleanly in C++, JavaScript, and Python.
📜 Problem Overview
You're given a string s
made of lowercase English letters.
Your task is to compute the maximum difference diff = a₁ - a₂
such that:
-
a₁
is the frequency of a character with odd frequency. -
a₂
is the frequency of a character with even frequency.
📌 Constraints:
3 <= s.length <= 100
-
s
contains at least one character with an odd frequency and one with an even frequency.
✏️ Example Walkthroughs
🧾 Example 1:
Input: s = "aaaaabbc"
Frequencies:
- a: 5 (odd)
- b: 2 (even)
- c: 1 (odd)
maxOdd = 5
minEven = 2
Output: 5 - 2 = 3
🧾 Example 2:
Input: s = "abcabcab"
Frequencies:
- a: 3 (odd)
- b: 3 (odd)
- c: 2 (even)
maxOdd = 3
minEven = 2
Output: 3 - 2 = 1
🧠 Intuition & Strategy
The idea is very simple:
- Count the frequency of each character.
- Find the maximum odd frequency (
maxOdd
). - Find the minimum even frequency (
minEven
). - Return
maxOdd - minEven
.
📌 We ignore characters that don’t appear at all (frequency = 0).
⚙️ C++ Code
class Solution {
public:
int maxDifference(string s) {
vector<int> count(26);
int maxOdd = 0;
int minEven = s.length();
for (const char c : s)
++count[c - 'a'];
for (const int freq : count) {
if (freq == 0)
continue;
if (freq % 2 == 0)
minEven = min(minEven, freq);
else
maxOdd = max(maxOdd, freq);
}
return maxOdd - minEven;
}
};
🌐 JavaScript Version
var maxDifference = function(s) {
let count = new Array(26).fill(0);
for (let char of s) {
count[char.charCodeAt(0) - 97]++;
}
let maxOdd = 0;
let minEven = s.length;
for (let freq of count) {
if (freq === 0) continue;
if (freq % 2 === 0) {
minEven = Math.min(minEven, freq);
} else {
maxOdd = Math.max(maxOdd, freq);
}
}
return maxOdd - minEven;
};
🐍 Python Version
class Solution:
def maxDifference(self, s: str) -> int:
from collections import Counter
freq = Counter(s)
max_odd = 0
min_even = len(s)
for count in freq.values():
if count % 2 == 0:
min_even = min(min_even, count)
else:
max_odd = max(max_odd, count)
return max_odd - min_even
🧪 Test Case Scenarios
Input: "aaaaabbc"
Output: 3 → 'a' (5), 'b' (2), maxOdd = 5, minEven = 2
Input: "abcabcab"
Output: 1 → 'a', 'b' (3 each), 'c' (2), maxOdd = 3, minEven = 2
Input: "aabbccdd"
Output: 2 → All have frequency 2 (even), so add 1 'a' to make it 3 → Now, maxOdd = 3, minEven = 2
Edge Case: "aabccc"
Output: 1 → 'a' (2), 'b' (1), 'c' (3), maxOdd = 3, minEven = 2
🧮 Time & Space Complexity
Time Complexity: O(n)
- One pass to count characters (O(n))
- Second pass through 26 letters (constant)
Space Complexity: O(1)
- Fixed-size array or map for 26 lowercase letters
✨ Wrap Up
This problem is a great example of frequency-based logic in string analysis.
It’s simple, efficient, and perfect for early technical interviews. 🧠
📈 If you're brushing up on data structure patterns like frequency maps or prefix logic, this one's a great addition to your toolkit.
If this article helped you, show it some love 💖, share it with peers, and follow me for more beginner-friendly algorithm deep dives!
Happy coding! 🧑💻🧠
Top comments (2)
Good Work
Thank you
Some comments may only be visible to logged-in visitors. Sign in to view all comments.