Skip to content

Instantly share code, notes, and snippets.

@rusyasoft
Created October 2, 2023 17:56
Show Gist options
  • Save rusyasoft/5458b8070477ffcea622a827cc1f8705 to your computer and use it in GitHub Desktop.
Save rusyasoft/5458b8070477ffcea622a827cc1f8705 to your computer and use it in GitHub Desktop.
2038. Remove Colored Pieces if Both Neighbors are the Same Color [Medium] https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color
class Solution {
public boolean winnerOfGame(String colors) {
int countA = 0, countB = 0;
int n = colors.length();
for (int i = 1; i < n - 1; i++) {
char left = colors.charAt(i - 1);
char middle = colors.charAt(i);
char right = colors.charAt(i + 1);
if (left == middle && middle == right) {
if (left == 'A') {
countA++;
} else {
countB++;
}
}
}
return countA > countB;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment