> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Remove All Adjacent Duplicates In String

Leetcode Problem 1047: Remove All Adjacent Duplicates In String

Problem Summary

Given a string s, repeatedly remove all adjacent duplicate pairs until no more exist. Return the final string. The order of removal does not affect the result.

Constraints:

  • The string has between 1 and 20,000 lowercase English characters.

Techniques

  • String
  • Stack

Solution

function removeDuplicates(s: string): string {
    let stack: string[] = []

    for(let i = 0; i < s.length; i++) {
        let currentChar = s.charAt(i)

        if (stack[stack.length - 1] !== currentChar) {
            stack.push(currentChar)
        } else {
            stack.pop()
        }
    }

    return stack.join("")
};