Backspace String Compare

Compare two strings after applying backspace edits, with the catch that the O(1) space version uses two pointers walking from the end.

EASY
Free
stack
two-pointers
strings
oliviafoster

By @oliviafoster

February 3, 2026

·

Updated May 20, 2026

188 views

5

4.5 (13)

Hit this on a phone screen at a fintech mid-stage in 2024, and the interviewer's follow-up was the part that tripped me up: solve it without building two new strings. The stack version is the obvious read of the problem, but the two-pointer scan from the end is the version that gets the senior signal.

Backspace String Compare

Given two strings s and t, return true if they are equal after each is processed as a sequence of typed characters and backspace edits, otherwise return false. The character '#' represents a backspace: when typed, it removes the previously typed character (if any). A backspace on an empty buffer is a no-op.

Examples

Example 1:

  • Input: s = "ab#c", t = "ad#c"
  • Output: true
  • Explanation: Both strings reduce to "ac".

Example 2:

  • Input: s = "ab##", t = "c#d#"
  • Output: true
  • Explanation: Both strings reduce to the empty string.

Example 3:

  • Input: s = "a#c", t = "b"
  • Output: false
  • Explanation: s reduces to "c" and t is "b", so they differ.

Example 4:

  • Input: s = "y#fo##f", t = "y#f#o##f"
  • Output: true
  • Explanation: Both reduce to "f".

Constraints

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

Follow-up

Can you solve the problem in O(n + m) time and O(1) extra space? The stack version is O(n + m) time but O(n + m) space, and the interviewer at the screen explicitly asked for the constant-space variant.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems