|
| 1 | +# Backspace String Compare |
| 2 | + |
| 3 | +Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a |
| 4 | +backspace character. |
| 5 | + |
| 6 | +Note that after backspacing an empty text, the text will continue empty. |
| 7 | + |
| 8 | +## Examples |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +Example 5: |
| 16 | + |
| 17 | +```text |
| 18 | +Input: s = "ab#c", t = "ad#c" |
| 19 | +Output: true |
| 20 | +Explanation: Both s and t become "ac". |
| 21 | +``` |
| 22 | + |
| 23 | +Example 6: |
| 24 | + |
| 25 | +```text |
| 26 | +Input: s = "ab##", t = "c#d#" |
| 27 | +Output: true |
| 28 | +Explanation: Both s and t become "". |
| 29 | +``` |
| 30 | + |
| 31 | +Example 7: |
| 32 | +```text |
| 33 | +Input: s = "a#c", t = "b" |
| 34 | +Output: false |
| 35 | +Explanation: s becomes "c" while t becomes "b". |
| 36 | +``` |
| 37 | + |
| 38 | +## Constraints |
| 39 | + |
| 40 | +- 1 <= s.length, t.length <= 200 |
| 41 | +- s and t only contain lowercase letters and '#' characters. |
| 42 | + |
| 43 | +> Follow up: Can you solve it in O(n) time and O(1) space? |
| 44 | +
|
| 45 | +## Topics |
| 46 | + |
| 47 | +- Two Pointers |
| 48 | +- String |
| 49 | +- Stack |
| 50 | +- Simulation |
| 51 | + |
| 52 | +## Solution(s) |
| 53 | + |
| 54 | +- [Two Pointers](#two-pointers) |
| 55 | +- [Build String](#build-string) |
| 56 | + |
| 57 | +### Two Pointers |
| 58 | + |
| 59 | +When writing a character, it may or may not be part of the final string depending on how many backspace keystrokes occur |
| 60 | +in the future. |
| 61 | + |
| 62 | +If instead we iterate through the string in reverse, then we will know how many backspace characters we have seen, and |
| 63 | +therefore whether the result includes our character. |
| 64 | + |
| 65 | +The key insight is that backspace characters affect only the characters to their left, which means if we traverse both |
| 66 | +strings from right to left, we can determine which characters are truly “visible” (i.e., not cancelled by a '#') and |
| 67 | +compare them on the fly without ever building the final strings. We maintain two pointers, one for each string, and a |
| 68 | +skip counter for each that tracks how many upcoming characters should be skipped due to pending backspaces. Whenever both |
| 69 | +pointers land on a valid character simultaneously, we compare them directly and move on. |
| 70 | + |
| 71 | +#### Algorithm |
| 72 | + |
| 73 | +Iterate through the string in reverse. If we see a backspace character, the next non-backspace character is skipped. If |
| 74 | +a character isn't skipped, it is part of the final answer. |
| 75 | + |
| 76 | +#### Complexity Analysis |
| 77 | + |
| 78 | +- **Time Complexity**: O(m + n), where m, n are the lengths of `s` and `t` respectively. Because each character in `s` |
| 79 | + (of length `m`) and each character in `t` of length `n` is visited at most once by its respective pointer, making the |
| 80 | + total work proportional to the combined length of both strings |
| 81 | +- **Space Complexity**: O(1). This is because only a fixed number of variables(the pointers) are used regardless of the |
| 82 | + input sizes, with no auxiliary data structures or string reconstruction required. |
| 83 | + |
| 84 | +### Build String |
| 85 | + |
| 86 | +Let's individually build the result of each string (build(S) and build(T)), then compare if they are equal. |
| 87 | + |
| 88 | +Algorithm |
| 89 | + |
| 90 | +To build the result of a string build(S), we'll use a stack based approach, simulating the result of each keystroke. |
| 91 | + |
| 92 | +#### Complexity Analysis |
| 93 | + |
| 94 | +- Time Complexity: O(M+N), where M,N are the lengths of S and T respectively. |
| 95 | +- Space Complexity: O(M+N). |
0 commit comments