Skip to main content

Maximum Difference Between Even and Odd Frequency I - Solution & Explanation

EasyHash TableStringCounting8 min readAsked at: Amazon, Microsoft, Meta +1
Practice this problem

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:

  • a1 has an odd frequency in the string.
  • a2 has 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 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 <= 100
  • s consists only of lowercase English letters.
  • s contains 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.

Solution

We can use a hash table or an array cnt to record the occurrences of each character in the string s. Then, we traverse cnt to find the maximum frequency a of characters that appear an odd number of times and the minimum frequency b of characters that appear an even number of times. Finally, we return a - b.

The time complexity is O(n), where n is the length of the string s. The space complexity is O(|\Sigma|), where \Sigma is the character set. In this problem, |\Sigma| = 26.

Code

Python

Java

C++

Go

TypeScript

Rust

C#

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Frequency ComparisonO(n + k^2)O(k)Useful for understanding the problem before optimizing
Counting with Extreme TrackingO(n + k)O(k)Best general solution for strings using frequency counting

Video Solution

Maximum Difference Between Even and Odd Frequency I | Easy | Leetcode 3442 | codestorywithMIK • codestorywithMIK • 4,556 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Difference Between Even and Odd Frequency I easy or hard?
Maximum Difference Between Even and Odd Frequency I is categorized as an Easy problem. The main concept is basic frequency counting on strings and checking whether counts are odd or even, which is a common beginner-friendly interview pattern.
Maximum Difference Between Even and Odd Frequency I Python/Java solution
Most implementations use a dictionary or array to count characters, then scan the counts to find the largest odd and smallest even values. The logic is identical across Python, Java, C++, Go, and TypeScript, differing only in syntax for maps and loops.
How to solve Maximum Difference Between Even and Odd Frequency I in O(n)?
First count the frequency of each character in the string using an array or hash map. Then iterate through those counts and track two values: the largest odd frequency and the smallest even frequency. Compute their difference if both exist. Each step scans the data once, giving O(n) overall complexity.
What is the best approach for Maximum Difference Between Even and Odd Frequency I?
The best approach is frequency counting with extreme tracking. Count occurrences of each character, then find the maximum odd frequency and the minimum even frequency. The final answer is maxOdd - minEven. This runs in O(n) time with O(k) space, where k is the number of unique characters.
Is Maximum Difference Between Even and Odd Frequency I asked at Google/Amazon/Meta?
Frequency counting and parity-based string problems are common in interviews at companies like Amazon, Google, and Meta. While this exact question may not appear frequently, the underlying pattern—counting characters and comparing frequency properties—is a standard interview topic.
What data structure is used in Maximum Difference Between Even and Odd Frequency I?
The main data structure is a hash table or fixed-size frequency array. It stores the count of each character in the string. After counting, simple variables track the maximum odd and minimum even frequencies.
What is the time complexity of Maximum Difference Between Even and Odd Frequency I?
The optimal solution runs in O(n + k) time. n is the length of the string used to build the frequency map, and k is the number of unique characters scanned to find the maximum odd and minimum even frequencies. Space complexity is O(k).

Ready to solve this problem?

Practice Maximum Difference Between Even and Odd Frequency I with our built-in code editor and test cases.

Practice on FleetCode