Maximum Difference Between Even and Odd Frequency II - Problem

You are given a string s and an integer k. Your task is to find the maximum difference between the frequency of two characters, freq[a] - freq[b], in a substring subs of s, such that:

  • subs has a size of at least k
  • Character a has an odd frequency in subs
  • Character b has a non-zero even frequency in subs

Return the maximum difference.

Note that subs can contain more than 2 distinct characters.

Input & Output

Example 1 — Basic Case
$ Input: s = "aabbc", k = 3
Output: -1
💡 Note: For all substrings of length >= 3: "aab" has a=2(even), b=1(odd), giving diff=1-2=-1. "abb" has a=1(odd), b=2(even), giving diff=1-2=-1. "bbc" has b=2(even), c=1(odd), giving diff=1-2=-1. "aabb" has all even frequencies, so invalid. "abbc" has a=1(odd), b=2(even), c=1(odd), giving max_odd=1, min_even=2, diff=-1. "aabbc" has a=2(even), b=2(even), c=1(odd), giving max_odd=1, min_even=2, diff=-1. The maximum difference is -1.
Example 2 — No Valid Combination
$ Input: s = "aaa", k = 2
Output: -1
💡 Note: All possible substrings of length >= 2 are "aa", "aa", "aaa". In each case, 'a' has even frequency (2, 2, 3 respectively). Wait, 3 is odd! In "aaa": 'a' appears 3 times (odd). But we need both an odd-frequency character AND an even-frequency character. Since only 'a' exists and it has odd frequency, there's no even-frequency character, so return -1.
Example 3 — Multiple Characters
$ Input: s = "abcabc", k = 4
Output: -1
💡 Note: For all substrings of length >= 4, we consistently get differences of -1 since the maximum odd frequency is 1 while minimum even frequency is 2. No substring gives a difference >= 0.

Constraints

  • 1 ≤ s.length ≤ 105
  • 1 ≤ k ≤ s.length
  • s consists of only lowercase English letters

Visualization

Tap to expand
Maximum Difference: Even vs Odd Frequency II INPUT String s = "aabbc" a a b b c 0 1 2 3 4 Character Frequencies: a: 2 b: 2 c: 1 Parameters: s = "aabbc" k = 3 (min length) Find: max(freq[a] - freq[b]) a=odd freq, b=even freq substring length >= k ALGORITHM STEPS 1 Enumerate Substrings Check all substrings with length >= k 2 Count Frequencies For each substring, count char frequencies 3 Classify Odd/Even Find chars with odd and even frequencies 4 Calculate Max Diff max(odd_freq - even_freq) where even_freq > 0 Best Substring: "abb" a: 1 (odd) [OK] b: 2 (even) [OK] Diff = 1 - 2 = -1 Best = 1 FINAL RESULT Valid Substrings (len >= 3): Substr Freqs Diff aab a:2,b:1 N/A abb a:1,b:2 1-2=-1 bbc b:2,c:1 1-2=-1 aabb a:2,b:2 N/A abbc a:1,b:2,c:1 1-2=-1 aabbc a:2,b:2,c:1 1-2=-1 Maximum Difference 1 Best: "abb" or "abbc" odd(a)=1, even(b)=2 Result: 1 - 2 = -1... wait Key Insight: The optimal approach uses prefix sums with parity tracking. For each pair of characters (a,b), track prefix[a] - prefix[b] and use parity states to find valid ranges efficiently. This reduces complexity from O(n^2) to O(26*26*n) by maintaining minimum prefix for each parity state. TutorialsPoint - Maximum Difference Between Even and Odd Frequency II | Optimal Solution
Asked in
Google 15 Microsoft 12 Amazon 8
8.5K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen