Skip to main content
Back to problems
#2027
Easy Algorithms

Minimum moves to convert string

String Greedy
57.7% acceptance
Feb 25, 2026
536
81
You are given a string s of 'X' and 'O'. A move selects three consecutive chars and converts them to 'O'. Return the minimum number of moves to convert all chars to 'O'.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_moves(s: String) -> i32 {
    let s: Vec<u8> = s.bytes().collect();
    let mut i = 0;
    let mut moves = 0;
    while i < s.len() {
      if s[i] == b'X' {
        moves += 1;
        i += 3;
      } else {
        i += 1;
      }
    }
    moves
  }
}