Skip to content

Commit daf4a3e

Browse files
authored
Create remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp
1 parent c89c447 commit daf4a3e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool winnerOfGame(string colors) {
7+
int cnt1 = 0, cnt2 = 0;
8+
for (int i = 1; i + 1 < size(colors); ++i) {
9+
if (!(colors[i - 1] == colors[i] && colors[i] == colors[i + 1])) {
10+
continue;
11+
}
12+
if (colors[i] == 'A') {
13+
++cnt1;
14+
} else {
15+
++cnt2;
16+
}
17+
}
18+
return cnt1 > cnt2;
19+
}
20+
};

0 commit comments

Comments
 (0)