Created
October 2, 2023 17:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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