Backspace String Compare

easy

Compare two strings with backspace characters (#)

Backspace String Compare

Key Insight

Process from end: count # as skips, find next valid char in each string, compare them.

Step 1Setup
t="ad#c"
a
0
b
1
#
2
i
j
c
3
same direction

s="ab#c", t="ad#c". Process from end using skip counters.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Backspace String Compare

Process from end: count # as skips, find next valid char in each string, compare them.

  1. Setup

    s="ab#c", t="ad#c". Process from end using skip counters.

  2. Compare Last Chars

    s[3]="c", t[3]="c". Match! Decrement both. i=2, j=2.

  3. Process Backspaces

    s[2]="#" → skip count=1, skip "b", land on i=0. t[2]="#" → skip "d", land on j=0.

  4. Final Compare

    s[0]="a", t[0]="a". Match! Both pointers exhausted.

  5. Result

    Both strings reduce to "ac". They are equal.