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's in s to convert.
Constraints:
- 3 <= s.length <= 1000
- s[i] is either 'X' or 'O'.
class Solution:
def minimumMoves(self, s: str) -> int:
count=0
while len(s)>=3:
if s[0]=="O":
s=s[1:]
elif s[0]=="X" or s[1]=="X" or s[2]=="X":
count+=1
s=s[3:]
if "X" in s:
count+=1
return count
'알고리즘 문제 > Leetcode' 카테고리의 다른 글
2160. Minimum Sum of Four Digit Number After Splitting Digits (0) | 2022.12.05 |
---|---|
2144. Minimum Cost of Buying Candies With Discount (0) | 2022.12.04 |
1974. Minimum Time to Type Word Using Special Typewriter (0) | 2022.12.02 |
1903. Largest Odd Number in String (0) | 2022.12.02 |
1827. Minimum Operations to Make the Array Increasing (0) | 2022.12.02 |