Apply Backspaces, Compare
L3 Easy Two Pointers
Concept
A stack is a fine way to model the backspace key: each
# deletes the most recently typed character.A text editor treats '#' as a backspace: it deletes the character immediately before it (consecutive '#' delete more). Given two strings s and t, return true if both strings end up identical after all backspaces are applied. An empty string stays empty when its last character is deleted.
Examples
▸ s = "ab#c", t = "ad#c"
→ true
▸ s = "ab##", t = "c#d#"
→ true
▸ s = "a#c", t = "b"
→ false
Progressive Hints
Hint 1 · Nudge
Process each string with a list you push onto and pop from.
Hint 2 · Plan
For each string, walk left to right with a result list: push a letter, and on '#' pop the last letter if any exists. Build both results and compare them for equality.
Hint 3 · Approach
build(x): for each c in x: if c == '#' and the list is non-empty, pop, otherwise if c != '#' push c. Return build(s) == build(t).
All hints are out. Take a breath and give it a shot.
Output
// Run your code to see the output here.