You are given a string s
consisting of n
characters which are either 'X'
or 'O'
.
A move is defined as selecting three consecutive characters of s
and converting them to 'O'
. Note that if a move is applied to the character 'O'
, it will stay the same.
Return the minimum number of moves required so that all the characters of s
are converted to 'O'
.
Input: s = "XXX" Output: 1 Explanation: XXX -> OOO We select all the 3 characters and convert them in one move.
Input: s = "XXOX" Output: 2 Explanation: XXOX -> OOOX -> OOOO We select the first 3 characters in the first move, and convert them to 'O'. Then we select the last 3 characters and convert them so that the final string contains all 'O's.
Input: s = "OOOO" Output: 0 Explanation: There are no 'X's in s to convert.
3 <= s.length <= 1000
s[i]
is either'X'
or'O'
.
impl Solution {
pub fn minimum_moves(s: String) -> i32 {
let s = s.as_bytes();
let mut i = 0;
let mut ret = 0;
while i < s.len() {
if s[i] == b'X' {
i += 2;
ret += 1;
}
i += 1;
}
ret
}
}