2154. Minimum Moves to Convert String
My accepted Python solution to LeetCode problem 2154, Minimum Moves to Convert String, running in 1ms.
- Difficulty: Easy
- Python
- Runtime 1ms
- Memory 17.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1ms, memory 17.3MB, accepted 2025-12-29.
class Solution:
def minimumMoves(self, s: str) -> int:
# Greedy: when we find 'X', convert 3 chars and skip
# Time: O(n), Space: O(1)
moves = 0
i = 0
while i < len(s):
if s[i] == 'X':
moves += 1
i += 3 # Skip next 3 chars (they all become 'O')
else:
i += 1
return moves