#844

Backspace String Compare

Easy Stack / backwards scan

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

In two strings, '#' means backspace. Decide whether they're equal after applying the backspaces.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: are the strings equal after applying '#' as backspace?

Why this works: build each final string with a stack: push letters, pop on '#'.

Example: "ab#c" and "ad#c"

"ab#c" → "ac", "ad#c" → "ac" → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Popping from an empty stack when the string starts with '#'.

Solve on LeetCode ↗
Complexity
  • Time: O(n + m).
  • Space: O(n + m) with the stack version.
Topic: Stack & Queue
🎬 Episodes 46–52: video coming soon