Backspace String Compare

Given two strings s and t, return true if they are equal when both are typed into empty text editors, where '#' means a backspace character. Backspacing an empty text leaves it empty. Follow-up: solve it in O(n) time and O(1) space.

30:00

Backspace String Compare
easy
Free written solution
Topics
Companies

Given two strings s and t, return true if they are equal when both are typed into empty text editors, where '#' means a backspace character. Backspacing an empty text leaves it empty. Follow-up: solve it in O(n) time and O(1) space.

Example 1:
Input: {"s":"ab#c","t":"ad#c"}
Output: true
Constraints:
  • 1≤s.length,t.length≤2001 \leq s.\text{length}, t.\text{length} \leq 200

  • s and t only contain lowercase letters and '#' characters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"ab#c","t":"ad#c"}

Scanning both strings from the right

'#' backspace

erased

survivor

s

t

a

b

⌫

c

a

d

⌫

c

i 3

j 3

Backspaces owed

skip_s

0

skip_t

0

Each '#' adds one; the next letter to its left pays one back and is erased.

Survivors paired, right to left

No pair compared yet.

Variables
VariableValue
i3
j3
skip_s0
skip_t0
DepthFunction Call
Stack empty
0/14