If you see two consecutive characters that are the same, you pop them from left to right, until you cannot pop any more characters. Return the resulting string. let str = "abba"
"abba" - pop the two b's -> "aa"
"aa" - pop the two a's -> ""
return ""
Here's what i have tried so far:
function match(str){
for (let i = 0; i< str.length; i++){
if (str[i] === str[i+1]){
return str.replace(str[i], "");
}
}
};
match('abba');
It removes one b only. On first loop, i want it to remove two b's and console the output. On second i want the remaining two a's to remove and console the output. Also It would be great if good time complexity is maintained.