☑99.54% || Cpp || Java || python || javascript || Greedy 💯
Read article Explaination and codes : https://www.nileshblog.tech/leetcode-2038-remove-colored-pieces/\n\nJava \nC++\nPython \nJavascript\n\n# the Problem\n Lee
# Read article Explaination and codes : https://www.nileshblog.tech/leetcode-2038-remove-colored-pieces/\n\nJava \nC++\nPython \nJavascript\n\n# the Problem\n Leetcode 2038 presents us with an array of colored pieces, each represented by a lowercase letter. Our goal is to remove all pieces with the same color if they have neighbors with the same color. In simpler terms, imagine a row of beads, and we want to remove adjacent beads of the same color. We\'ll keep doing this until no more removals are possible.\n \n ![image](https://assets.leetcode.com/users/images/4e9d1c39-dc61-4a08-81cd-41fdcac14949_1696208433.910032.png)\n\n \n To solve this problem, we\'ll use a greedy strategy. Greedy algorithms aim to make the best decision at each step, leading to an optimal solution. In our case, we\'ll repeatedly scan the array to find and remove adjacent pieces with the same color until we can\'t make any more removals.\n **time complexity is O(N)**\n ![image](https://www.nileshblog.tech/wp-content/uploads/2023/10/Leetcode-2038.-Remove-Colored-Pieces-if-Both-Neighbors-are-the-Same-Color-725x1024.jpeg)\n step-by-step\n1. Initialize an vector / array cnts to keep track of the points collected by both players. Initially, both players have zero points.\n1. Initialize currrent to the first color in the string, and count to zero to keep track of the consecutive balls of the current color.\n1. Iterate through the string of colors:\n1. If the current ball has the same color as the previous one, increment count.\n1. If count reaches or exceeds 3, update the score in the cnts array for the current player (\'A\' or \'B\').\n1. If the current ball has a different color, reset cur to the new color and reset count to 1.\n1. After processing the entire string, the function returns true if player \'A\' (represented by cnts[0]) has more points than player \'B\' (represented by cnts[1]), indicating that player \'A\' is the winner.\n\n![image](https://assets.leetcode.com/users/images/fe8cb493-787f-4ec1-95e1-cbfd61651295_1696206957.9743786.png)\n
['C', 'Python', 'Java', 'JavaScript']