Backspace String Compare Visualization & Animation

Simulates backspace characters and compares two strings using two pointers from the end; O(n) time, O(1) space.

## What is it? Compare two strings after simulating the backspace character (`#`). Characters followed by `#` are deleted. Determine if the final strings (after all backspaces) are equal. ## How it works **Two-pointer from the end (optimal):** - Use two pointers starting at the end of each string - Track a `skip` count for backspace characters encountered - Move each pointer backwards, skipping characters as indicated by `#` - Compare characters at both pointers when neither needs skipping - Return false if they differ, true if both pointers exhaust simultaneously **Stack approach:** - Push characters; pop on `#` - Compare final stacks ## When to use - String simulation problems involving "undo" or "delete" operations - Interview problems testing pointer manipulation and edge case handling ## Key Points - Two-pointer from the end: O(n) time, O(1) space — optimal - Stack approach: O(n) time, O(n) space — simpler to understand - Multiple consecutive `#` must be handled correctly

Category: algorithms

Difficulty: beginner

Time Complexity: O(n)

Space Complexity: O(1)

Backspace String Compare

beginner

Simulates backspace characters and compares two strings using two pointers from the end; O(n) time, O(1) space.

Init
push chars onto stack — '#' triggers backspace
s
a
0
b
1
#
2
c
3
empty
Stack S
t
a
0
d
1
#
2
c
3
empty
Stack T
s="ab#c", t="ad#c". Push chars onto a stack — '#' pops (backspace). Compare both results.