欢迎光临散文网 会员登陆 & 注册

Leetcode 2027. Minimum Moves to Convert String

2023-01-19 15:17 作者:您是打尖儿还是住店呢  | 我要投稿

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: 1Explanation: XXX -> OOO We select all the 3 characters and convert them in one move.

Example 2:

Input: s = "XXOX"Output: 2Explanation: 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: 0Explanation: There are no 'X's in s to convert.

 

Constraints:

  • 3 <= s.length <= 1000

  • s[i] is either 'X' or 'O'.

因为只能就近 连续3个,所以只要找到一个X,它后面2个不管是不是,就直接算进去就行的。也就是i=i+3;step++;

然后如果没有,就i++;依次往后面遍历即可;

思路对了,做起来就很方便了。

Runtime0 ms

Beats

100%

Memory39.9 MB

Beats

98.73%


Leetcode 2027. Minimum Moves to Convert String的评论 (共 条)

分享到微博请遵守国家法律