Skip to content

Commit c8fcdaa

Browse files
authored
Create 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.kt
1 parent 42d0f6d commit c8fcdaa

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// count the number of "AAA" and "BBB"
2+
class Solution {
3+
fun winnerOfGame(s: String): Boolean {
4+
var alice = 0
5+
var bob = 0
6+
7+
for (i in 1 until s.lastIndex) {
8+
if (s[i - 1] == s[i] && s[i] == s[i + 1]) {
9+
if (s[i] == 'A')
10+
alice++
11+
if (s[i] == 'B')
12+
bob++
13+
}
14+
}
15+
16+
return alice > bob
17+
}
18+
}
19+
20+
// or alternativly count the picks directly
21+
class Solution {
22+
fun winnerOfGame(s: String): Boolean {
23+
var colorA = 0
24+
var i = 0
25+
while (i < s.length) {
26+
var j = i
27+
while (j < s.length && s[j] == 'A') j++
28+
if (j - i > 2) colorA += (j - i - 2)
29+
i = ++j
30+
}
31+
32+
var colorB = 0
33+
i = 0
34+
while (i < s.length) {
35+
var j = i
36+
while (j < s.length && s[j] == 'B') j++
37+
if (j - i > 2) colorB += (j - i - 2)
38+
i = ++j
39+
}
40+
41+
return if (colorA - colorB > 0) true else false
42+
}
43+
}

0 commit comments

Comments
 (0)