Maximum Difference Between Even and Odd Frequency II - Problem

You're given a string s and an integer k. Your challenge is to find the maximum difference between character frequencies in any substring of length at least k.

Specifically, you need to find freq[a] - freq[b] where:

  • The substring has at least k characters
  • Character a appears an odd number of times
  • Character b appears a non-zero even number of times

Think of it as finding the most "unbalanced" pair of characters in a valid substring!

Example: In substring "aaabbc", character 'a' appears 3 times (odd) and 'b' appears 2 times (even), giving a difference of 3 - 2 = 1.

Input & Output

example_1.py — Basic Case
$ Input: s = "aaabbc", k = 3
› Output: 1
šŸ’” Note: In substring 'aaabbc' (length 6 ≄ k=3), character 'a' appears 3 times (odd) and 'b' appears 2 times (even). The difference is 3-2 = 1.
example_2.py — Multiple Options
$ Input: s = "abccba", k = 4
› Output: 1
šŸ’” Note: In substring 'abccba' (length 6 ≄ k=4), character 'c' appears 2 times (even) and 'a' or 'b' appear 2 times each (even). We need odd-even pair, so we look at shorter valid substrings where we can find the best difference of 1.
example_3.py — Edge Case
$ Input: s = "aab", k = 3
› Output: 1
šŸ’” Note: The only valid substring is 'aab' itself. Character 'a' appears 2 times (even) and 'b' appears 1 time (odd). Since we need odd-even difference, it's 1-2 = -1, but we want max, so difference is 1.

Visualization

Tap to expand
Maximum Difference Between Even and Odd FrequencyStep 1: Input AnalysisString: "aaabbcc" k=4 (minimum substring length)Valid substrings: "aaab", "aaabb", "aaabbc", "aaabbcc", "aabbc", etc.Step 2: Frequency AnalysisSubstring "aaabbcc":• a: 3 times (odd)• b: 2 times (even)• c: 2 times (even)Step 3: Find Maximum DifferenceOdd frequencies: [3]Even frequencies: [2, 2]Max difference: 3 - 2 = 1āœ“ Answer: 1Algorithm OptimizationšŸ”§ Brute Force: Check all O(n²) substrings → O(n³) time⚔ Sliding Window: Expand window incrementally → O(n²) timešŸ“Š Key insight: Reuse frequency counts as window expands✨ Result: Efficient solution with smart frequency tracking⚔
Understanding the Visualization
1
Identify Valid Segments
Find all continuous segments of length at least k characters
2
Count Character Frequencies
For each segment, count how many times each character appears
3
Classify Odd vs Even
Separate characters by whether their frequency is odd or even
4
Calculate Maximum Difference
Find the largest gap: max(odd_frequency) - min(even_frequency)
Key Takeaway
šŸŽÆ Key Insight: Use sliding window with frequency maps to efficiently track character counts and find optimal odd-even pairs without redundant calculations, reducing time complexity from O(n³) to O(n²).

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

O(n) possible starting positions, each with O(n) possible ending positions, but with optimized frequency tracking

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Frequency map size is bounded by alphabet size (constant)

n
2n
āœ“ Linear Space

Constraints

  • 1 ≤ k ≤ s.length ≤ 105
  • s consists of lowercase English letters only
  • Must find substring of length at least k
  • Character with odd frequency and character with even frequency must both exist in the chosen substring
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
24.5K Views
Medium Frequency
~25 min Avg. Time
892 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