3445. Maximum Difference Between Even and Odd Frequency II
Difficulty: Hard
Topics: String
, Sliding Window
, Enumeration
, Prefix Sum
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[^1] subs
of s
, such that:
-
subs
has a size of at leastk
. - Character
a
has an odd frequency insubs
. - Character
b
has an even frequency insubs
.
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 is1 - 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 is3 - 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
Hint:
- Fix the two characters.
- Use prefix sum (maintain 2 characters' parities as status).
Solution:
We need to find the maximum difference between the frequency of two characters, a
and b
, in any substring of the given string s
such that:
- The substring has a size of at least
k
. - The frequency of character
a
is odd. - The frequency of character
b
is even.
Approach
Problem Analysis: The problem requires examining all possible substrings of
s
with length at leastk
to find the maximum difference between the frequency of a character with an odd count and another character with an even count. Given the constraints, a brute-force approach is infeasible. Instead, we use a sliding window technique combined with prefix sums and state enumeration for efficiency.Key Insight: For each pair of distinct characters
(a, b)
from the set{'0', '1', '2', '3', '4'}
, we can use prefix sums to track the cumulative counts ofa
andb
as we iterate through the string. We maintain a sliding window to ensure the substring length is at leastk
and contains both characters.State Management: We use a 2x2 matrix
minDiff
to store the minimum difference between the prefix counts ofa
andb
for each possible parity combination (even or odd) of their counts. This helps in efficiently computing the required difference for valid substrings.Sliding Window: As we iterate through each character in the string, we expand the window to include the current character and adjust the left boundary of the window to maintain the constraints (substring length ≥
k
and presence of botha
andb
). For each valid window, we update theminDiff
matrix based on the parities of the prefix counts at the left boundary.Candidate Calculation: For each position, we compute the potential maximum difference by considering the current counts of
a
andb
and the stored minimum differences fromminDiff
for the required parities (odd fora
and even forb
).
Let's implement this solution in PHP: 3445. Maximum Difference Between Even and Odd Frequency II
<?php
/**
* @param String $s
* @param Integer $k
* @return Integer
*/
function maxDifference($s, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo maxDifference("12233", 4) . "\n"; // Output: -1
echo maxDifference("1122211", 3) . "\n"; // Output: 1
echo maxDifference("110", 3) . "\n"; // Output: -1
?>
Explanation:
-
Generating Permutations: We generate all distinct pairs of characters from the set
{'0', '1', '2', '3', '4'}
to consider all possible combinations of charactersa
andb
. -
Prefix Arrays: For each character pair, we maintain two prefix arrays,
prefixA
andprefixB
, which store the cumulative counts of charactersa
andb
up to each position in the string. -
Sliding Window: We use a sliding window approach to ensure the substring length is at least
k
and contains both charactersa
andb
. The left boundaryl
is adjusted dynamically to maintain these conditions. -
State Management: The
minDiff
matrix tracks the minimum difference between the prefix counts ofa
andb
for each possible parity combination (even or odd). This helps in efficiently computing the required difference for valid substrings. -
Candidate Calculation: For each position, we calculate the potential maximum difference by considering the current counts of
a
andb
and the relevant entry from theminDiff
matrix, updating the global maximum difference (ans
) whenever a larger value is found.
This approach efficiently narrows down the problem to manageable computations by leveraging sliding windows, prefix sums, and state enumeration, ensuring optimal performance even for large input sizes.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)