1888. Minimum Number of Flips to Make the Binary String Alternating Medium

@problem@discussion
#String#Dynamic Programming#Greedy#Sliding Window



1/**
2 * [1888] Minimum Number of Flips to Make the Binary String Alternating
3 *
4 * You are given a binary string s. You are allowed to perform two types of operations on the string in any sequence:
5 * 
6 * 	Type-1: Remove the character at the start of the string s and append it to the end of the string.
7 * 	Type-2: Pick any character in s and flip its value, i.e., if its value is '0' it becomes '1' and vice-versa.
8 * 
9 * Return the minimum number of type-2 operations you need to perform such that s becomes alternating.
10 * The string is called alternating if no two adjacent characters are equal.
11 * 
12 * 	For example, the strings "010" and "1010" are alternating, while the string "0100" is not.
13 * 
14 *  
15 * Example 1:
16 * 
17 * Input: s = "111000"
18 * Output: 2
19 * Explanation: Use the first operation two times to make s = "100011".
20 * Then, use the second operation on the third and sixth elements to make s = "10<u>1</u>01<u>0</u>".
21 * 
22 * Example 2:
23 * 
24 * Input: s = "010"
25 * Output: 0
26 * Explanation: The string is already alternating.
27 * 
28 * Example 3:
29 * 
30 * Input: s = "1110"
31 * Output: 1
32 * Explanation: Use the second operation on the second element to make s = "1<u>0</u>10".
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	1 <= s.length <= 10^5
38 * 	s[i] is either '0' or '1'.
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/
44// discuss: https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn min_flips(s: String) -> 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_1888() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.