Problem 394
Backspace String Compare
Easy · 10 pts · String, Stack, Two Pointers
Problem
Two strings are typed into empty text editors, where the character '#' means pressing backspace: it deletes the character just before it, if there is one. A backspace on an empty text does nothing. Decide whether the two typed strings end up equal. A stack solution is straightforward; walking both strings from the end with two pointers solves it with O(1) extra memory.
Input
Two lines: strings S and T made of lowercase letters and '#'.
Output
Print YES if the final texts are equal, otherwise NO.
Constraints
1 <= |S|, |T| <= 200
Example input
ab#c ad#c
Example output
YES
Explanation
'ab#c' types a, b, deletes b, then types c, giving 'ac'. 'ad#c' also gives 'ac', so the answer is YES.