3445. Maximum Difference Between Even and Odd Frequency II Hard

@problem@discussion
#String#Sliding Window#Enumeration#Prefix Sum



1/**
2 * [3445] Maximum Difference Between Even and Odd Frequency II
3 *
4 * 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 <span data-keyword="substring">substring</span> subs of s, such that:
5 * 
6 * 	subs has a size of at least k.
7 * 	Character a has an odd frequency in subs.
8 * 	Character b has an even frequency in subs.
9 * 
10 * Return the maximum difference.
11 * Note that subs can contain more than 2 distinct characters.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">s = "12233", k = 4</span>
16 * Output: <span class="example-io">-1</span>
17 * Explanation:
18 * For the substring "12233", the frequency of '1' is 1 and the frequency of '3' is 2. The difference is 1 - 2 = -1.
19 * </div>
20 * <strong class="example">Example 2:
21 * <div class="example-block">
22 * Input: <span class="example-io">s = "1122211", k = 3</span>
23 * Output: <span class="example-io">1</span>
24 * Explanation:
25 * For the substring "11222", the frequency of '2' is 3 and the frequency of '1' is 2. The difference is 3 - 2 = 1.
26 * </div>
27 * <strong class="example">Example 3:
28 * <div class="example-block">
29 * Input: <span class="example-io">s = "110", k = 3</span>
30 * Output: <span class="example-io">-1</span>
31 * </div>
32 *  
33 * Constraints:
34 * 
35 * 	3 <= s.length <= 3 * 10^4
36 * 	s consists only of digits '0' to '4'.
37 * 	The input is generated that at least one substring has a character with an even frequency and a character with an odd frequency.
38 * 	1 <= k <= s.length
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-ii/
44// discuss: https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-ii/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn max_difference(s: String, k: i32) -> i32 {
50        0
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_3445() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.