[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:
bool backspaceCompare(string s, string t) {
return buildString(s) == buildString(t);
}
private:
string buildString(string& str) {
string result;
for(char c : str) {
if(c == '#') {
if(!result.empty()) {
result.pop_back();
}
} else {
result.push_back(c);
}
}
return result;
}
};
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)