1864. Minimum Number of Swaps to Make the Binary String Alternating Medium

@problem@discussion
#String#Greedy



1/**
2 * [1864] Minimum Number of Swaps to Make the Binary String Alternating
3 *
4 * Given a binary string s, return the minimum number of character swaps to make it alternating, or -1 if it is impossible.
5 * The string is called alternating if no two adjacent characters are equal. For example, the strings "010" and "1010" are alternating, while the string "0100" is not.
6 * Any two characters may be swapped, even if they are not adjacent.
7 *  
8 * Example 1:
9 * 
10 * Input: s = "111000"
11 * Output: 1
12 * Explanation: Swap positions 1 and 4: "1<u>1</u>10<u>0</u>0" -> "1<u>0</u>10<u>1</u>0"
13 * The string is now alternating.
14 * 
15 * Example 2:
16 * 
17 * Input: s = "010"
18 * Output: 0
19 * Explanation: The string is already alternating, no swaps are needed.
20 * 
21 * Example 3:
22 * 
23 * Input: s = "1110"
24 * Output: -1
25 * 
26 *  
27 * Constraints:
28 * 
29 * 	1 <= s.length <= 1000
30 * 	s[i] is either '0' or '1'.
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating/
36// discuss: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn min_swaps(s: String) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_1864() {
54    }
55}
56


Back
© 2025 bowen.ge All Rights Reserved.