-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_color_game.go
26 lines (24 loc) · 1.17 KB
/
remove_color_game.go
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
package problem2038
/*
There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'.
You are given a string colors of length n where colors[i] is the color of the ith piece.
Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.
Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'.
She is not allowed to remove pieces that are colored 'B'.
Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'.
He is not allowed to remove pieces that are colored 'A'.
Alice and Bob cannot remove pieces from the edge of the line.
If a player cannot make a move on their turn, that player loses and the other player wins.
Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.
*/
func winnerOfGame(colors string) bool {
var score int
for i := 1; i < len(colors)-1; i++ {
if colors[i-1] == 'A' && colors[i] == 'A' && colors[i+1] == 'A' {
score++
} else if colors[i-1] == 'B' && colors[i] == 'B' && colors[i+1] == 'B' {
score--
}
}
return score > 0
}