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 substringsubs of s, such that:
subs has a size of at leastk.
Character a has an odd frequency in subs.
Character b has an even frequency in subs.
Return the maximum difference.
Note that subs can contain more than 2 distinct characters.
Example 1:
Input:s = "12233", k = 4
Output:-1
Explanation:
For the substring "12233", the frequency of '1' is 1 and the frequency of '3' is 2. The difference is 1 - 2 = -1.
Example 2:
Input:s = "1122211", k = 3
Output:1
Explanation:
For the substring "11222", the frequency of '2' is 3 and the frequency of '1' is 2. The difference is 3 - 2 = 1.
Example 3:
Input:s = "110", k = 3
Output:-1
Constraints:
3 <= s.length <= 3 * 104
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
Solutions
Solution 1: Enumerate Character Pairs + Sliding Window + Prefix State Compression
We want to find a substring \(\textit{subs}\) of string \(s\) that satisfies the following conditions:
The length of \(\textit{subs}\) is at least \(k\).
The number of occurrences of character \(a\) in \(\textit{subs}\) is odd.
The number of occurrences of character \(b\) in \(\textit{subs}\) is even.
Maximize the frequency difference \(f_a - f_b\), where \(f_a\) and \(f_b\) are the number of occurrences of \(a\) and \(b\) in \(\textit{subs}\), respectively.
The characters in \(s\) are from '0' to '4', so there are 5 possible characters. We can enumerate all different character pairs \((a, b)\), for a total of at most \(5 \times 4 = 20\) combinations. We define:
Character \(a\) is the target character with odd frequency.
Character \(b\) is the target character with even frequency.
We use a sliding window to maintain the left and right boundaries of the substring, with variables:
\(l\) denotes the position before the left boundary, so the window is \([l+1, r]\);
\(r\) is the right boundary, traversing the entire string;
\(\textit{curA}\) and \(\textit{curB}\) denote the number of occurrences of \(a\) and \(b\) in the current window;
\(\textit{preA}\) and \(\textit{preB}\) denote the cumulative occurrences of \(a\) and \(b\) before the left boundary \(l\).
We use a 2D array \(t[2][2]\) to record the minimum value of \(\textit{preA} - \textit{preB}\) for each possible parity combination of the window's left end, where \(t[i][j]\) means \(\textit{preA} \bmod 2 = i\) and \(\textit{preB} \bmod 2 = j\).
Each time we move \(r\) to the right, if the window length satisfies \(r - l \ge k\) and \(\textit{curB} - \textit{preB} \ge 2\), we try to move the left boundary \(l\) to shrink the window, and update the corresponding \(t[\textit{preA} \bmod 2][\textit{preB} \bmod 2]\).
In this way, we can compute the maximum frequency difference for the current window each time \(r\) moves to the right.
The time complexity is \(O(n \times |\Sigma|^2)\), where \(n\) is the length of \(s\) and \(|\Sigma|\) is the alphabet size (5 in this problem). The space complexity is \(O(1)\).