## 1758. Minimum Changes To Make Alternating Binary String

You are given a string `s` consisting only of the characters `'0'` and `'1'`. In one operation, you can change any `'0'` to `'1'` or vice versa.

The string is called alternating if no two adjacent characters are equal. For example, the string `"010"` is alternating, while the string `"0100"` is not.

Return the minimum number of operations needed to make `s` alternating.

Example 1:

```Input: s = "0100"
Output: 1
Explanation: If you change the last character to '1', s will be "0101", which is alternating.
```

Example 2:

```Input: s = "10"
Output: 0
```

Example 3:

```Input: s = "1111"
Output: 2
Explanation: You need two operations to reach "0101" or "1010".
```

Constraints:

• `1 <= s.length <= 104`
• `s[i]` is either `'0'` or `'1'`.

## Rust Solution

``````struct Solution;

impl Solution {
fn min_operations(s: String) -> i32 {
let mut odd = [0, 0];
let mut even = [0, 0];
let s: Vec<char> = s.chars().collect();
let n = s.len();
for i in 0..n {
if i % 2 == 0 {
if s[i] == '0' {
even += 1;
} else {
even += 1;
}
} else {
if s[i] == '0' {
odd += 1;
} else {
odd += 1;
}
}
}
let a = (even - (n + 1) as i32 / 2).abs() + (odd - n as i32 / 2).abs();
let b = (odd - n as i32 / 2).abs() + (even - (n + 1) as i32 / 2).abs();
a.min(b)
}
}

#[test]
fn test() {
let s = "0100".to_string();
let res = 1;
assert_eq!(Solution::min_operations(s), res);
let s = "10".to_string();
let res = 0;
assert_eq!(Solution::min_operations(s), res);
let s = "1111".to_string();
let res = 2;
assert_eq!(Solution::min_operations(s), res);
}
``````

Having problems with this solution? Click here to submit an issue on github.