You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 3445. Maximum Difference Between Even and Odd Frequency II
2
+
// 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:
3
+
// subs has a size of at least k.
4
+
// Character a has an odd frequency in subs.
5
+
// Character b has a non-zero even frequency in subs.
6
+
// Return the maximum difference.
7
+
// Note that subs can contain more than 2 distinct characters.
8
+
9
+
10
+
// Solution: Prefix Sum & Sliding Window
11
+
12
+
// Enumerate each pair of (i, j), where i is the character with odd frequency and j is the even one.
13
+
14
+
// Maintain a sliding window of at least size k, and contains at least 2 occurances of j.
15
+
// Keep track of:
16
+
// Prefix sum of the current difference (count i - count j).
17
+
// Parity of occurances of i and j.
18
+
// Store the minimum score for each key (parity of count of i, parity of count of j), for indices that we are moving out of the window.
19
+
// To find the maximum difference, find the maximum (current score - minimum score with a key of parity (flipped, same)).
0 commit comments