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'sinsto convert.
Constraints:
3 <= s.length <= 1000s[i] is either 'X' or 'O'.Problem Overview: You are given a string consisting of characters 'X' and 'O'. In one move, you can convert three consecutive characters starting from an index into 'O'. The goal is to find the minimum number of moves required to convert the entire string so that no 'X' remains.
Approach 1: Recursive Backtracking (Inefficient) (Time: O(3^n), Space: O(n))
This method explores every possible way to apply conversion moves. At each index containing 'X', the algorithm tries performing a move that flips the current character and the next two positions to 'O'. The recursion continues until the string contains no 'X'. Because multiple overlapping states are explored and the same subproblems repeat frequently, the number of recursive calls grows exponentially. This approach mainly helps you reason about the brute-force search space and understand why a more efficient strategy is needed.
Approach 2: Greedy Approach (Time: O(n), Space: O(1))
The key observation is that when you encounter an 'X', the optimal move is always to convert the current position and the next two characters immediately. Any solution must eventually cover this 'X', and delaying the move never reduces the number of operations. Iterate through the string from left to right. When you see 'X', increment the move counter and skip the next two indices because a single operation already converts them to 'O'. When you see 'O', simply move to the next character. This linear scan ensures each character is processed once, making the algorithm efficient and simple.
The greedy strategy works because every operation affects a fixed window of three characters, and covering the earliest uncovered 'X' immediately always leads to the minimum number of operations. The problem naturally fits a greedy pattern combined with sequential scanning of a string.
Recommended for interviews: The greedy solution is what interviewers expect. It demonstrates that you can identify the local decision that guarantees a global optimum and implement it with a single pass. Mentioning the brute-force or backtracking idea first shows understanding of the problem space, but implementing the O(n) greedy scan shows strong algorithmic judgment.
The greedy approach involves scanning through the string, and whenever an 'X' is encountered, converting three consecutive characters to 'O'. This method ensures that each move is used optimally, converting the maximum number of characters possible in a single move to minimize the total count.
This C solution defines a function minimumMoves that iterates over the string. When an 'X' is found, it increments the move counter and jumps forward by 3 indices to apply the conversion.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as no additional space is used other than a few variables.
Though not the most optimal solution, a recursive backtracking method can explore all possibilities. This checks all potential moves, backtracks when necessary, and might help understand the problem domain and provide a deterministic method if implemented carefully with memoization.
The recursive version examines each index and decides whether to convert based on the presence of 'X'. Moves are incremented recursively down the string.
Time Complexity: O(n), but with recursion overhead.
Space Complexity: O(n), due to call stack with recursion.
Traverse the string s. Whenever you encounter 'X', move the pointer i three steps forward and add 1 to the answer; otherwise, move the pointer i one step forward.
The time complexity is O(n), where n represents the length of the string s.
| Approach | Complexity |
|---|---|
| Greedy Approach | Time Complexity: O(n), where n is the length of the string. |
| Recursive Backtracking (Inefficient) | Time Complexity: O(n), but with recursion overhead. |
| Greedy Algorithm | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Backtracking (Inefficient) | O(3^n) | O(n) | Useful for understanding the brute-force search space or explaining why optimization is required |
| Greedy Scan | O(n) | O(1) | Best choice for interviews and production code; processes the string in one pass |
2027. Minimum Moves to Convert String | LEETCODE EASY • code Explainer • 1,141 views views
Watch 9 more video solutions →Practice Minimum Moves to Convert String with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor