Skip to content
Decoder

Backspace String Compare

Easy

Problem

Two strings s and t are typed into blank text editors. The character # means backspace: it deletes the character before it, if there is one (backspacing an empty editor does nothing). Return true if both editors end up showing the same text.

Example 1

Input
s = "ab#c", t = "ad#c"
Output
true
Why
both become "ac"

Example 2

Input
s = "ab##", t = "c#d#"
Output
true
Why
both become ""

Example 3

Input
s = "a#c", t = "b"
Output
false
Why
"c" vs "b"

Can you do it in O(1) extra space, without building the final strings?

Constraints

  • 1 <= s.length, t.length <= 200
  • s and t contain only lowercase letters and #.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.