> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Difference Between Even and Odd

> Tested Python solution for LeetCode 3442 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 3442, [Easy](/catalog/easy). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3442   # by problem number
lcpy gen -s maximum_difference_between_even_and_odd_frequency_i   # by problem name
```

## Problem

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.

### Examples

```
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.
```

```
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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_difference_between_even_and_odd_frequency_i/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_difference_between_even_and_odd_frequency_i/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_difference(self, s: str) -> int:
        freqs = Counter(s).values()
        return max(f for f in freqs if f % 2 == 1) - min(f for f in freqs if f % 2 == 0)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.