Talentd
FleetCode

2027. Minimum Moves to Convert String

Easy58.4% acceptance3 companies

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'.

Example 1:

Input: s = "XXX"
Output: 1
Explanation: XXX -> OOO
We select all the 3 characters and convert them in one move.

Example 2:

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.

Example 3:

Input: s = "OOOO"
Output: 0
Explanation: There are no 'X's in s to convert.

Constraints:

  • 3 <= s.length <= 1000
  • s[i] is either 'X' or 'O'.

Asked at 3 companies

Hints open one at a time

Hint 1
Find the smallest substring you need to consider at a time.
Hint 2
Try delaying a move as long as possible.
4:57
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minimumMoves(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"XXX"
Input
"XXOX"
Input
"OOOO"
Your code is saved on this device.Solution