Skip to content

1047. Remove All Adjacent Duplicates In String

Edited by Charles

Easy · Python · Stack

Problem

Read the original problem on LeetCode.

The official page contains the problem statement, examples, and constraints.

Explanation, examples & other approaches

Core idea

This question is quite similar to 20. Valid Parentheses. Iterate the whole string.

Original study notes follow, including intermediate snippets and earlier attempts.

Note:

  1. This question is quite similar to 20. Valid Parentheses.
  2. Iterate the whole string.
    1. If the character is not in the top of the stack list or the stack list is empty, append it. For example, “axxazy” will result in empty stack list before the pointer comes through s[4].
    2. Else, cancel the repeated character in the stack list.
Study record
Field Value
Attempts 1
Mastery ❚
The Latest Attempt January 9, 2026

Python solution

class Solution:
    def removeDuplicates(self, s: str) -> str:
        n = len(s)
        stack = [s[0]]

        for i in range(1, n):
            if not stack or s[i] != stack[-1]:
                stack.append(s[i])
            else:
                stack.pop()

        return ''.join(stack)

Back to Day 9 Stack & Queue I · All Algorithm notes