File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments