Compare Strings After Simulating Backspace Characters
Implement compareStringsAfterBackspaces
Given two strings
s and t, where # means a backspace, return true if typing each string into an empty text editor produces the same result.
Building each string's final contents with a stack works — push regular characters, pop on '#' — but it costs extra storage the size of both inputs. Scanning from the end of each string avoids that: resolve each pointer to the next character that survives every backspace to its right (skipping the '#' itself and however many characters it deletes), then compare those two resolved characters directly. A mismatch — or one string running out before the other — settles the answer immediately, all without ever building a final string.
Example 1:
Input: s = "ab#c", t = "ad#c"
Output: true
Example 2:
Input: s = "ab##", t = "c#d#"
Output: true
Example 3:
Input: s = "a#c", t = "b"
Output: false
+ 8 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ s.length, t.length ≤ 200 - ●
s and t consist of lowercase English letters and the character '#' (backspace)
s =
ab#c
t =
ad#c