Skip to content

Practice Maximum Difference Between Even and Odd Frequency II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3445
LeetCode Problem

Maximum Difference Between Even and Odd Frequency II

Problem Statement

Given s and k, consider substrings of length at least k. In a chosen substring, select a character a with odd frequency and a different character b with positive even frequency. Maximize freq[a] - freq[b] and return that difference. The substring may contain other characters as well.

Source: Maximum Difference Between Even and Odd Frequency II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "12233", k = 4
Output
-1

# Example 2

Input
s = "1122211", k = 3
Output
1

# Example 3

Input
s = "110", k = 3
Output
-1

Constraints

3 <= s.length <= 3 * 10^4
s consists only of digits '0' to '4'.
The input is generated that at least one substring has a character with an even frequency and a character with an odd frequency.
1 <= k <= s.length

Related LeetCode Problems

Practice more problems similar to Maximum Difference Between Even and Odd Frequency II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.