How to remove all adjacent duplicate characters from a string until no more can be removed in TypeScript

1 Answer

0 votes
function removeAdjacentDuplicates(s: string): string {
    const stack: string[] = [];

    for (const ch of s) {
        if (stack.length > 0 && stack[stack.length - 1] === ch) {
            stack.pop();          // pop
        } else {
            stack.push(ch);       // push
        }
    }

    return stack.join("");
}

const s = "abbacccada";

console.log(removeAdjacentDuplicates(s));   




/*
run:

"cada" 

*/

 



answered Mar 7 by avibootz

Related questions

...