-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2027-MinimumMovesToConvertString.go
More file actions
67 lines (57 loc) · 1.89 KB
/
2027-MinimumMovesToConvertString.go
File metadata and controls
67 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
// 2027. 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's in s to convert.
// Constraints:
// 3 <= s.length <= 1000
// s[i] is either 'X' or 'O'.
import "fmt"
func minimumMoves(s string) int {
res := 0
for i := 0; i < len(s); {
if s[i] == 'O' {
i++
} else {
res++
i += 3
}
}
return res
}
func main() {
// Example 1:
// Input: s = "XXX"
// Output: 1
// Explanation: XXX -> OOO
// We select all the 3 characters and convert them in one move.
fmt.Println(minimumMoves("XXX")) // 1
// 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.
fmt.Println(minimumMoves("XXOX")) // 2
// Example 3:
// Input: s = "OOOO"
// Output: 0
// Explanation: There are no 'X's in s to convert.
fmt.Println(minimumMoves("OOOO")) // 0
}