[Easy] 844. Backspace String Compare
Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
Thinking Process
- Backwards Processing: Processing from right to left handles backspaces naturally
- Stack matches nested or LIFO structure (parentheses, monotonic scans).
- Push on open / larger; pop when the current element resolves pending work.
- Monotonic stack finds next greater/smaller in O(n).
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Monotonic stack (this problem) | O(n) | O(n) | Next greater/smaller element |
| Parentheses matching | O(n) | O(n) | Push open, pop on close |
| Expression evaluation | O(n) | O(n) | Operand + operator stacks |
| Stack simulation | O(n) | O(n) | Process in LIFO order |
Examples
Example 1:
Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
Example 2:
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".
Example 3:
Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".
Constraints
1 <= s.length, t.length <= 200sandtonly contain lowercase letters and'#'characters.
Alternative Approach: Stack-Based
class Solution {
public boolean backspaceCompare(String s, String t) {
// 1. String construct: ab#c backwards construct: ca
// Request: O(n) time O(1) space . in space update.
// 2. backwars, 2 pointers: if # move 2 backwards, then compare
int i = s.length() -1, j = t.length() - 1;
int skipS = 0, skipT = 0;
while(i >= 0 || j >= 0) {
while(i >) {
if(s.charAt(i) == '#') {skipS++; i--;}
else if(skipS > 0) {skipS--; i--;}
else break;
}
while(j >) {
if(t.charAt(j) == '#') {skipT++; j--;}
else if(skipT > 0) {skipT--; j--;}
else break;
}
if(i >= 0 && j>= 0 && s.charAt(i) != t.charAt(j)) return false;
i--;
j--;
}
return i == j;
}
}
Time Complexity: O(n + m)
Space Complexity: O(n + m)
Comparison:
- Stack-based: Simpler to understand, but uses O(n + m) space
- Two Pointers: More complex, but O(1) space - better for large inputs
Common Mistakes
- All backspaces:
s = "###",t = "##"→ both become"", returntrue - Empty strings:
s = "",t = ""→ returntrue - Backspace at start:
s = "#a",t = "a"→ both become"a", returntrue - Different lengths:
s = "a#b",t = "b"→ both become"b", returntrue -
No backspaces:
s = "abc",t = "abc"→ returntrue - Wrong skip logic: Not properly handling consecutive backspaces
- Index errors: Off-by-one errors when moving pointers
- Missing break: Not breaking from inner loops when finding actual character
- Wrong comparison: Comparing before processing all backspaces
- Return condition: Not checking
i == jcorrectly (both should be-1)
Related Problems
- LC 1047: Remove All Adjacent Duplicates In String - Similar stack-based pattern
- LC 1209: Remove All Adjacent Duplicates in String II - K duplicates version
- LC 1544: Make The String Great - Similar character removal pattern
Key Takeaways
- Backwards Processing: Processing from right to left handles backspaces naturally
- Skip Counter: Tracks characters to skip due to backspaces
- Character Matching: Only compares actual characters after handling backspaces
- Space Efficiency: Two-pointer approach achieves O(1) space
References
- LC 844: Backspace String Compare on LeetCode
- LeetCode Discuss — LC 844: Backspace String Compare
- LeetCode Editorial (may require premium)