Skip to main content
Back to problems
Leetcode
Medium
Stacks
Strings
Two Pointers
Backspace String Compare

Compare two strings after interpreting # as a backspace character.

Acceptance 0%
Problem Statement

You are given two strings. Each string may contain lowercase letters and the # character. Treat # as a backspace: it deletes the character immediately before it, if one exists.

Return whether the two strings are equal after all backspaces are applied.

Input Format

  • Two strings s and t.
  • Each string consists of lowercase English letters and #.

Output Format

  • Return true if the two strings become identical after processing backspaces.
  • Otherwise, return false.

Constraints

  • The strings may be empty.
  • A # can remove a previous character only if one exists.
  • Aim for an O(n+m)O(n + m) time solution and O(1)O(1) or near-constant extra space if possible.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "ab#c", t = "ad#c"

Output

true

Explanation

After applying backspaces, both strings become "ac".

Example 2

Input

s = "ab##", t = "c#d#"

Output

true

Explanation

Both strings reduce to the empty string.

Show 1 more example

Example 3

Input

s = "a#c", t = "b"

Output

false

Explanation

The first string becomes "c", while the second remains "b".

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.