原文链接: https://leetcode-cn.com/problems/minimum-moves-to-convert-string
英文原文
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'.
中文题目
给你一个字符串 s ,由 n 个字符组成,每个字符不是 'X' 就是 'O' 。
一次 操作 定义为从 s 中选出 三个连续字符 并将选中的每个字符都转换为 'O' 。注意,如果字符已经是 'O' ,只需要保持 不变 。
返回将 s 中所有字符均转换为 'O' 需要执行的 最少 操作次数。
示例 1:
输入:s = "XXX"
输出:1
解释:XXX -> OOO
一次操作,选中全部 3 个字符,并将它们转换为 'O' 。
示例 2:
输入:s = "XXOX" 输出:2 解释:XXOX -> OOOX -> OOOO 第一次操作,选择前 3 个字符,并将这些字符转换为'O'。 然后,选中后 3 个字符,并执行转换。最终得到的字符串全由字符'O'组成。
示例 3:
输入:s = "OOOO"
输出:0
解释:s 中不存在需要转换的 'X' 。
提示:
3 <= s.length <= 1000s[i]为'X'或'O'
通过代码
高赞题解
双百喜欢炫耀
思路
1.将字符串转换成字符数组
2.从头遍历字符数组,如果该字符为'O'直接跳过,否则就对后面三个进行修改
3.注意边界问题哦

public int minimumMoves(String s) {
int n = s.length();
int count = 0;
char[] chars = s.toCharArray();
int i = 0;
while(i < n){
if(chars[i] == 'O') {
i++;
continue;
}
int k = 3;
while(k > 0 && i < n){
chars[i++] = 'O';
k--;
}
count++;
}
return count;
}
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 4867 | 8714 | 55.9% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|