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.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= s.length, t.length <= 200
s
andt
only contain lowercase letters and '#' characters.
Solutions
The key idea is to process the strings as if they were being typed into a text editor, where the backspace character removes the previous character.
Approach – Brute Force Technique
This algorithm compares two strings after processing backspaces in them. It uses two stacks to simulate the typing and backspacing process. For each character in the strings, if it's a backspace and the stack is not empty, the top character is removed. If it's not a backspace, the character is added to the stack. After processing both strings, it compares the resulting stacks. If they are equal, it returns true
; otherwise, it returns false
.
Steps
Check for null or empty strings: If either
s
ort
is null or empty, the method returnstrue
. Ifs
is null or empty, it returnsfalse
. Ift
is null or empty, it also returnsfalse
.Initialize variables: A character variable
backspace
is initialized with the value'#'
. Two stackssStack
andtStack
are created to hold the characters ofs
andt
respectively.Process string
s
: For each character ins
, if the character is a backspace andsStack
is not empty, the top character is popped fromsStack
. If the character is not a backspace, it is pushed ontosStack
.Process string
t
: The same process is repeated fort
, but withtStack
.Compare stack sizes: If the sizes of
sStack
andtStack
are not equal, the method returnsfalse
because the processed strings are of different lengths.Compare stacks: For each item in
sStack
, the method pops an item fromtStack
and compares the two. If they are not equal, the method returnsfalse
.Return true: If all the above checks pass, the method returns
true
, indicating that the processed stringss
andt
are equal.
Complexity
Time complexity:
O(S+T)
Space complexity:
O(S+T)
Approach – Two Pointers Technique
Uses two pointers, one for each string, starting from the end and moving backwards. When a pointer encounters a backspace character, it moves back until it finds a non-backspace character. The algorithm then compares these characters. If they're not the same, it returns false
. If all compared characters are the same, it returns true
.
Steps
Initialize two pointers: Start with two pointers, one at the end of each string.
Iterate over the strings: Move the pointers backwards through the strings. If a pointer encounters a backspace character, it keeps moving backwards until it finds a non-backspace character. This simulates the effect of the backspace.
Compare characters: Once both pointers are pointing to non-backspace characters, compare these characters. If they're not the same, return
false
.Repeat until pointers meet the start of the strings: Repeat steps 2 and 3 until both pointers have reached the start of their respective strings.
Return true: If all compared characters were the same, return
true
.
Complexity
Time complexity:
O(S+T)
Space complexity:
O(1)
Last updated