You are given a string s
consisting of lowercase English letters. A move consists of choosing any 3
consecutive characters of s
and converting them to 'XXX'
. Note that the characters in the string do not necessarily need to be distinct. Return the minimum number of moves required so that all the characters of s
are converted to 'X'
.
Example:
s = "XXX"
, Output: 0
s = "XXXT"
, Output: 1
s = "TXXTXX"
, Output: 2
'X'
.'XXX'
.def minimumMoves(s: str) -> int:
moves = 0
i = 0
while i < len(s):
if s[i] != 'X':
moves += 1
i += 3 # Skip the next 3 characters because they will be converted to 'XXX'
else:
i += 1 # Move to the next character to check for non-'X'
return moves
# Example Usage
print(minimumMoves("XXX")) # Output: 0
print(minimumMoves("XXXT")) # Output: 1
print(minimumMoves("TXXTXX")) # Output: 2
s
. We traverse each character in the string once, skipping ahead by 3 indices whenever a move is made.This strategy ensures we efficiently count the minimum number of moves needed to convert the string.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?