3399. Smallest Substring With Identical Characters II Hard

@problem@discussion
#String#Binary Search



1/**
2 * [3399] Smallest Substring With Identical Characters II
3 *
4 * You are given a binary string s of length n and an integer numOps.
5 * You are allowed to perform the following operation on s at most numOps times:
6 * 
7 * 	Select any index i (where 0 <= i < n) and flip s[i]. If s[i] == '1', change s[i] to '0' and vice versa.
8 * 
9 * You need to minimize the length of the longest <span data-keyword="substring-nonempty">substring</span> of s such that all the characters in the substring are identical.
10 * Return the minimum length after the operations.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">s = "000001", numOps = 1</span>
15 * Output: <span class="example-io">2</span>
16 * Explanation: 
17 * By changing s[2] to '1', s becomes "001001". The longest substrings with identical characters are s[0..1] and s[3..4].
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">s = "0000", numOps = 2</span>
22 * Output: <span class="example-io">1</span>
23 * Explanation: 
24 * By changing s[0] and s[2] to '1', s becomes "1010".
25 * </div>
26 * <strong class="example">Example 3:
27 * <div class="example-block">
28 * Input: <span class="example-io">s = "0101", numOps = 0</span>
29 * Output: <span class="example-io">1</span>
30 * </div>
31 *  
32 * Constraints:
33 * 
34 * 	1 <= n == s.length <= 10^5
35 * 	s consists only of '0' and '1'.
36 * 	0 <= numOps <= n
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/smallest-substring-with-identical-characters-ii/
42// discuss: https://leetcode.com/problems/smallest-substring-with-identical-characters-ii/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn min_length(s: String, num_ops: i32) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_3399() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.