Minimum Moves to Convert String - Problem

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 & Output

Example 1 — Basic Case
$ Input: s = "XXX"
Output: 1
💡 Note: We can convert all three X's to O's with a single move: one move converts "XXX" to "OOO"
Example 2 — Mixed Characters
$ Input: s = "XXOX"
Output: 2
💡 Note: First move at position 0 converts "XXOX" to "OOOX", second move at position 3 converts it to "OOOO"
Example 3 — Already Converted
$ Input: s = "OOOO"
Output: 0
💡 Note: All characters are already 'O', so no moves are needed

Constraints

  • 1 ≤ s.length ≤ 1000
  • s[i] is either 'X' or 'O'

Visualization

Tap to expand
Minimum Moves to Convert StringInput:XXOXEach move converts 3 consecutive characters to OMove 1OOOXMove 2Result: 2 moves
Understanding the Visualization
1
Input
String with X's and O's
2
Process
Apply moves to convert 3 consecutive chars to O
3
Output
Minimum number of moves needed
Key Takeaway
🎯 Key Insight: Greedily convert X's from left to right - delaying never helps!
Asked in
Microsoft 25 Amazon 20
28.5K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen