“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

describe

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.
Copy the code

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.
Copy the code

Example 3:

Input: s = "OOOO"
Output: 0
Explanation: There are no 'X's in s to convert.
Copy the code

Note:

3 <= s.length <= 1000
s[i] is either 'X' or 'O'.
Copy the code

parsing

Select three consecutive characters at a time. If the character is X, the character is O. If the character is O, the character is O.

While the problem may seem a bit confusing, it’s actually quite simple to figure it out, as follows:

  • Initialize a result where result is 0 and index I is 0
  • When len(s)
  • When the loop ends, result is returned

answer

class Solution(object):
    def minimumMoves(self, s):
        """
        :type s: str
        :rtype: int
        """
        result = 0
        i = 0
        while i<len(s):
            if s[i]=='O':
                i += 1
                continue
            else:
                result += 1
                i += 3
        return result
      
		
Copy the code

The results

Given each node in the Python online submission list with Minimum Moves to Convert String. Memory Usage: The linked submissions in the Python online list for Minimum Moves to Convert strings.Copy the code

Original link: leetcode.com/problems/mi…

Your support is my biggest motivation