↓Skip to main content
  1. LeetCode/

LeetCode 844: Backspace String Compare

·2 mins· ·
LeetCode Easy Two-Pointers String Stack Simulation
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-15

  • Submissions: Runtime Error → Wrong Answer → Accepted ×2 → Runtime Error ×5 → Time Limit Exceeded ×3 → Runtime Error ×3 → Accepted

Approach
#

With a stack (Stack Template 2): push ordinary characters and pop on # (doing nothing when empty), then compare the stacks.

For O(1) space walk from the end, since # only affects earlier characters. Each string keeps skip: a # adds one, a character with skip > 0 is skipped and decrements it, until the next surviving character is found; then compare the two.

My Attempts
#

I wrote this in 2022 and 2026 and failed 13 times in total:

  • The first 2022-09 version used stacks and popped an empty stack when # came first (IndexError).
  • Then came O(1)-space two pointers starting at len(S) (out of range), with inner while loops that never advanced, looping forever: three TLEs.
  • A 2026 two-pointer rewrite read s[j] for the t side in two versions (an unedited copy-paste); t[j] passed.

Solution
#

class Solution:
    def backspaceCompare(self, s: str, t: str) -> bool:
        def next_char(x, i):
            skip = 0
            while i >= 0:
                if x[i] == "#":
                    skip += 1
                elif skip:
                    skip -= 1
                else:
                    return i
                i -= 1
            return -1
        i, j = len(s) - 1, len(t) - 1
        while True:
            i, j = next_char(s, i), next_char(t, j)
            if i < 0 or j < 0:
                return i < 0 and j < 0
            if s[i] != t[j]:
                return False
            i, j = i - 1, j - 1

Complexity
#

Time O(n + m), space O(1).

Mistakes to Avoid
#

  • A # on an empty text does nothing; check before popping.
  • Each round of the two-pointer loop must advance the pointers, or it never ends.
  • Do not mix up the strings: compare s[i] with t[j].

Interview Explanation
#

The direct approach simulates backspaces with stacks. For O(1) space I walk both strings from the end, since a backspace only affects earlier characters, counting skips at each # and comparing the next surviving characters. Time O(n + m).

Related

LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum
LeetCode 680: Valid Palindrome II
·2 mins
LeetCode Easy Two-Pointers String Greedy
LeetCode 680 note: at the first mismatch the only options are deleting the left or the right character; check whether either remainder is a palindrome
LeetCode 696: Count Binary Substrings
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 696 note: split the string into runs of equal characters; each adjacent pair of runs contributes min(previous, current) substrings
LeetCode 541: Reverse String II
·1 min
LeetCode Easy Two-Pointers String
LeetCode 541 note: in every block of 2k reverse the first k; when fewer than k remain reverse them all, which slicing handles automatically
LeetCode 415: Add Strings
·2 mins
LeetCode Easy Math String Simulation
LeetCode 415 note: add digit by digit from the ends with two pointers, tracking the carry, and append any leftover carry after the loop
LeetCode 344: Reverse String
·1 min
LeetCode Easy Two-Pointers String
LeetCode 344 note: swap with two pointers and move inward to reverse in place