Problem summary: 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.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Two Pointers · Stack
Example 1
"ab#c"
"ad#c"
Example 2
"ab##"
"c#d#"
Example 3
"a#c"
"b"
Related Problems
Crawler Log Folder (crawler-log-folder)
Removing Stars From a String (removing-stars-from-a-string)
Step 02
Core Insight
What unlocks the optimal approach
No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #844: Backspace String Compare
class Solution {
public boolean backspaceCompare(String s, String t) {
int i = s.length() - 1, j = t.length() - 1;
int skip1 = 0, skip2 = 0;
for (; i >= 0 || j >= 0; --i, --j) {
while (i >= 0) {
if (s.charAt(i) == '#') {
++skip1;
--i;
} else if (skip1 > 0) {
--skip1;
--i;
} else {
break;
}
}
while (j >= 0) {
if (t.charAt(j) == '#') {
++skip2;
--j;
} else if (skip2 > 0) {
--skip2;
--j;
} else {
break;
}
}
if (i >= 0 && j >= 0) {
if (s.charAt(i) != t.charAt(j)) {
return false;
}
} else if (i >= 0 || j >= 0) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #844: Backspace String Compare
func backspaceCompare(s string, t string) bool {
i, j := len(s)-1, len(t)-1
skip1, skip2 := 0, 0
for ; i >= 0 || j >= 0; i, j = i-1, j-1 {
for i >= 0 {
if s[i] == '#' {
skip1++
i--
} else if skip1 > 0 {
skip1--
i--
} else {
break
}
}
for j >= 0 {
if t[j] == '#' {
skip2++
j--
} else if skip2 > 0 {
skip2--
j--
} else {
break
}
}
if i >= 0 && j >= 0 {
if s[i] != t[j] {
return false
}
} else if i >= 0 || j >= 0 {
return false
}
}
return true
}
# Accepted solution for LeetCode #844: Backspace String Compare
class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
i, j, skip1, skip2 = len(s) - 1, len(t) - 1, 0, 0
while i >= 0 or j >= 0:
while i >= 0:
if s[i] == '#':
skip1 += 1
i -= 1
elif skip1:
skip1 -= 1
i -= 1
else:
break
while j >= 0:
if t[j] == '#':
skip2 += 1
j -= 1
elif skip2:
skip2 -= 1
j -= 1
else:
break
if i >= 0 and j >= 0:
if s[i] != t[j]:
return False
elif i >= 0 or j >= 0:
return False
i, j = i - 1, j - 1
return True
// Accepted solution for LeetCode #844: Backspace String Compare
impl Solution {
pub fn backspace_compare(s: String, t: String) -> bool {
let (s, t) = (s.as_bytes(), t.as_bytes());
let (mut i, mut j) = (s.len(), t.len());
while i != 0 || j != 0 {
let mut skip = 0;
while i != 0 {
if s[i - 1] == b'#' {
skip += 1;
} else if skip != 0 {
skip -= 1;
} else {
break;
}
i -= 1;
}
skip = 0;
while j != 0 {
if t[j - 1] == b'#' {
skip += 1;
} else if skip != 0 {
skip -= 1;
} else {
break;
}
j -= 1;
}
if i == 0 && j == 0 {
break;
}
if i == 0 || j == 0 {
return false;
}
if s[i - 1] != t[j - 1] {
return false;
}
i -= 1;
j -= 1;
}
true
}
}
// Accepted solution for LeetCode #844: Backspace String Compare
function backspaceCompare(s: string, t: string): boolean {
let i = s.length - 1;
let j = t.length - 1;
while (i >= 0 || j >= 0) {
let skip = 0;
while (i >= 0) {
if (s[i] === '#') {
skip++;
} else if (skip !== 0) {
skip--;
} else {
break;
}
i--;
}
skip = 0;
while (j >= 0) {
if (t[j] === '#') {
skip++;
} else if (skip !== 0) {
skip--;
} else {
break;
}
j--;
}
if (s[i] !== t[j]) {
return false;
}
i--;
j--;
}
return true;
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(n)
Space
O(1)
Approach Breakdown
BRUTE FORCE
O(n²) time
O(1) space
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
TWO POINTERS
O(n) time
O(1) space
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Shortcut: Two converging pointers on sorted data → O(n) time, O(1) space.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
Moving both pointers on every comparison
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Breaking monotonic invariant
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.