02. Remove Colored Pieces if Both Neighbors are the Same Color
The problem can be found at the following link: Question Link
My Approach
Initialize scores for Alice and Bob (
aliceScore
andbobScore
) tozero
.Iterate through the colors (excluding edge pieces) using a loop. For each piece in the iteration:
Check if it's A and its neighboring pieces are also A. If yes, increment
aliceScore
as Alice can remove the current piece.Check if it's B and its neighboring pieces are also B. If yes, increment
bobScore
as Bob can remove the current piece.
Return
true
if Alice's score isgreater
than Bob's score; otherwise, returnfalse
.
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
Auxiliary Space Complexity:
O(1)
Code (C++)
class Solution {
public:
bool winnerOfGame(string colors) {
int alice=0;
int bob=0;
for(int i=1;i<colors.size()-1;i++)
{
if(colors[i-1]=='A' && colors[i+1]=='A' && colors[i]=='A')
{
alice++;
}
else if(colors[i-1]=='B' && colors[i+1]=='B' && colors[i]=='B')
{
bob++;
}
}
cout<<alice<<" "<<bob<<endl;
return alice>bob;
}
};
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the rishabhv12/Daily-Leetcode-Solution repository.
Last updated
Was this helpful?