Maximum Difference Between Even and Odd Frequency I
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given a string s consisting of lowercase English letters.
Your task is to find the maximum difference diff = a1 - 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.
Examples
Example 1
Input: s = "aaaaabbc"
Output: 3
Explanation:
* The character `'a'` has an **odd frequency** of `5`, and `'b'` has an **even frequency** of `2`.
* The maximum difference is `5 - 2 = 3`.
Example 2
Input: s = "abcabcab"
Output: 1
Explanation:
* The character `'a'` has an **odd frequency** of `3`, 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.
Solution
Method 1 – Frequency Counting and Max Difference
Intuition
The key idea is to count the frequency of each character in the string, then find the highest frequency among characters with odd counts (a1) and the lowest frequency among characters with even counts (a2). The answer is the maximum difference a1 - a2. This works because the problem asks for the largest possible difference between an odd and an even frequency.
Approach
- Initialize a frequency array or map to count occurrences of each character.
- Iterate through the string and update the frequency count for each character.
- Initialize variables to track the maximum odd frequency (
max_odd) and minimum even frequency (min_even). - Iterate through the frequency counts:
- If the frequency is odd and greater than
max_odd, updatemax_odd. - If the frequency is even and less than
min_even, updatemin_even.
- Return the difference
max_odd - min_even.
Code
C++
class Solution {
public:
int maxDifference(string s) {
int freq[26] = {0};
for (char c : s) freq[c - 'a']++;
int max_odd = 0, min_even = 101;
for (int f : freq) {
if (f == 0) continue;
if (f % 2 == 1 && f > max_odd) max_odd = f;
if (f % 2 == 0 && f < min_even) min_even = f;
}
return max_odd - min_even;
}
};
Go
type Solution struct{}
func (Solution) maxDifference(s string) int {
freq := [26]int{}
for _, c := range s {
freq[c-'a']++
}
maxOdd, minEven := 0, 101
for _, f := range freq {
if f == 0 {
continue
}
if f%2 == 1 && f > maxOdd {
maxOdd = f
}
if f%2 == 0 && f < minEven {
minEven = f
}
}
return maxOdd - minEven
}
Java
class Solution {
public int maxDifference(String s) {
int[] freq = new int[26];
for (char c : s.toCharArray()) freq[c - 'a']++;
int maxOdd = 0, minEven = 101;
for (int f : freq) {
if (f == 0) continue;
if (f % 2 == 1 && f > maxOdd) maxOdd = f;
if (f % 2 == 0 && f < minEven) minEven = f;
}
return maxOdd - minEven;
}
}
Kotlin
class Solution {
fun maxDifference(s: String): Int {
val freq = IntArray(26)
for (c in s) freq[c - 'a']++
var maxOdd = 0
var minEven = 101
for (f in freq) {
if (f == 0) continue
if (f % 2 == 1 && f > maxOdd) maxOdd = f
if (f % 2 == 0 && f < minEven) minEven = f
}
return maxOdd - minEven
}
}
Python
class Solution:
def maxDifference(self, s: str) -> int:
freq: list[int] = [0] * 26
for c in s:
freq[ord(c) - ord('a')] += 1
max_odd: int = 0
min_even: int = 101
for f in freq:
if f == 0:
continue
if f % 2 == 1 and f > max_odd:
max_odd = f
if f % 2 == 0 and f < min_even:
min_even = f
return max_odd - min_even
Rust
impl Solution {
pub fn max_difference(s: String) -> i32 {
let mut freq = [0; 26];
for b in s.bytes() {
freq[(b - b'a') as usize] += 1;
}
let mut max_odd = 0;
let mut min_even = 101;
for &f in &freq {
if f == 0 { continue; }
if f % 2 == 1 && f > max_odd { max_odd = f; }
if f % 2 == 0 && f < min_even { min_even = f; }
}
max_odd - min_even
}
}
Complexity
- ⏰ Time complexity:
O(n)wherenis the length of the string (counting frequencies and iterating over 26 letters). - 🧺 Space complexity:
O(1)(fixed-size frequency array).