Prompt: Write a function to crush candy in one dimensional board. In candy crushing games, groups of like items are removed from the board. In this problem, any sequence of 3 or more like items should be removed and any items adjacent to that sequence should now be considered adjacent to each other.
Input: “aaaabbbc”
Output: “c”
Explanation:
Remove 3 ‘a’: “aaaabbbbc” => “bbbbc”
Remove 4 ‘b’: “bbbbc” => “c”
here is my solution so far, im trying to use 2 pointer method, not sure where my logic is wrong, please help.
function candyCrush(s){
var n = s.length;
var j = 0;
var k = 1;
for(var i = 0; j < n; i++, j++)
{
s[i] = s[j];
if(i > 0 && s[i-1] == s[i])
{
k++;
}
else{
if(k >= 3)
{
i = i - (k+1);
j -= 1
k = 1;
}
else{
k = 1;
}
}
}
return s.substring(0,i+1);
}