AlgoDaily

Remove All Adjacent Duplicates In String

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Question

The Concept: Removing Adjacent Duplicates

Imagine you're attending a magical carnival where you can pull adjacent duplicate letters from a string like pulling rabbits out of a hat. Exciting, right? Your goal is to get a clean string with no adjacent duplicates. In this lesson, we'll explore this problem with Linda's candy adventure.

The Problem

Linda loves candies and messages, so she wants to write a message on her cake with candies. However, she can't have adjacent duplicate candies; they need to be unique. Can you help her?

Objective: Help Linda by removing all adjacent duplicate letters until you get a string with no adjacent duplicates.

Visual Walkthrough: Unpacking the Bag of Candies
  1. Starting String: Linda's bag of candies spells out "abbaca".

  2. Step 1: Spot the first adjacent duplicate "bb". These candies can't be together. Remove them!

- Result: "aaca"
  1. Step 2: Next, we have another adjacent duplicate "aa". Let's remove those as well.
- Result: "ca"

And voila! We're left with a string that doesn't have any adjacent duplicates, and it spells out "ca".

Visual Example

Constraints: The Boundaries of Our Magical Carnival
  • The length of the string s will be between 1 and (10^5).
  • The string s will only consist of lowercase English letters.

CONTINUE WITH ALGODAILY PREMIUM

Keep going with the complete tutorial.

Unlock the remaining explanations and exercises, alongside the full learning library.

Explore Premium →
Open interactive view →