Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Build confidence with an intuition-first walkthrough focused on greedy fundamentals.
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 summary: 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'.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"XXX"
"XXOX"
"OOOO"
minimum-cost-to-convert-string-i)minimum-cost-to-convert-string-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2027: Minimum Moves to Convert String
class Solution {
public int minimumMoves(String s) {
int ans = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == 'X') {
++ans;
i += 2;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2027: Minimum Moves to Convert String
func minimumMoves(s string) (ans int) {
for i := 0; i < len(s); i++ {
if s[i] == 'X' {
ans++
i += 2
}
}
return
}
# Accepted solution for LeetCode #2027: Minimum Moves to Convert String
class Solution:
def minimumMoves(self, s: str) -> int:
ans = i = 0
while i < len(s):
if s[i] == "X":
ans += 1
i += 3
else:
i += 1
return ans
// Accepted solution for LeetCode #2027: Minimum Moves to Convert String
impl Solution {
pub fn minimum_moves(s: String) -> i32 {
let s = s.as_bytes();
let n = s.len();
let mut ans = 0;
let mut i = 0;
while i < n {
if s[i] == b'X' {
ans += 1;
i += 3;
} else {
i += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #2027: Minimum Moves to Convert String
function minimumMoves(s: string): number {
const n = s.length;
let ans = 0;
let i = 0;
while (i < n) {
if (s[i] === 'X') {
ans++;
i += 3;
} else {
i++;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.