LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 1ms, memory 17.3MB, accepted 2025-12-29.

python
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

Source