Minimum Moves to Convert String

easy string greedy

Problem

You are given a string s consisting only of 'X' and 'O'. In one move you may pick any three consecutive characters and turn all of them into 'O'. Return the minimum number of moves required to convert s into all 'O'.

Inputs = "XXOX"
Output2
Convert s[0..2] then s[1..3] (or any equivalent). Greedily jumping by 3 on each X also yields 2.

def minimum_moves(s):
    moves = 0
    i = 0
    while i < len(s):
        if s[i] == 'X':
            moves += 1
            i += 3
        else:
            i += 1
    return moves
function minimumMoves(s) {
  let moves = 0;
  let i = 0;
  while (i < s.length) {
    if (s[i] === 'X') {
      moves++;
      i += 3;
    } else {
      i++;
    }
  }
  return moves;
}
class Solution {
    public int minimumMoves(String s) {
        int moves = 0, i = 0;
        while (i < s.length()) {
            if (s.charAt(i) == 'X') {
                moves++;
                i += 3;
            } else {
                i++;
            }
        }
        return moves;
    }
}
int minimumMoves(string s) {
    int moves = 0, i = 0;
    while (i < (int)s.size()) {
        if (s[i] == 'X') {
            moves++;
            i += 3;
        } else {
            i++;
        }
    }
    return moves;
}
Time: O(n) Space: O(1)