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:
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 <= 200
s
and t
only contain lowercase letters and '#'
characters.Follow up: Can you solve it in O(n)
time and O(1)
space?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method focuses on simplifying the problem first. For each string, simulate the effect of the backspaces to get the final form of the string, then just compare the final forms of the two strings.
Here's how the algorithm would work step-by-step:
def backspace_string_compare_brute_force(first_string, second_string):
def simplify_string(input_string):
simplified_string = []
for character in input_string:
if character != '#':
simplified_string.append(character)
# Handle backspace character
else:
# Removing the last character
if simplified_string:
simplified_string.pop()
return "".join(simplified_string)
# Simplifying the first string
simplified_first_string = simplify_string(first_string)
# Simplifying the second string
simplified_second_string = simplify_string(second_string)
# Comparing the two strings
return simplified_first_string == simplified_second_string
The trick is to process both strings backwards, one character at a time. The clever part is to keep track of backspaces encountered, skipping characters that should be deleted.
Here's how the algorithm would work step-by-step:
def backspace_string_compare(first_string, second_string):
first_string_pointer = len(first_string) - 1
second_string_pointer = len(second_string) - 1
first_string_backspace_count = 0
second_string_backspace_count = 0
while first_string_pointer >= 0 or second_string_pointer >= 0:
while first_string_pointer >= 0:
if first_string[first_string_pointer] == '#':
first_string_backspace_count += 1
first_string_pointer -= 1
elif first_string_backspace_count > 0:
first_string_backspace_count -= 1
first_string_pointer -= 1
else:
break
while second_string_pointer >= 0:
if second_string[second_string_pointer] == '#':
second_string_backspace_count += 1
second_string_pointer -= 1
elif second_string_backspace_count > 0:
second_string_backspace_count -= 1
second_string_pointer -= 1
else:
break
# If either pointer is still in bounds, compare chars
if first_string_pointer >= 0 and second_string_pointer >= 0:
if first_string[first_string_pointer] != second_string[second_string_pointer]:
return False
# Strings are not equal if one pointer is still in bounds
else:
if first_string_pointer >= 0 or second_string_pointer >= 0:
return False
first_string_pointer -= 1
second_string_pointer -= 1
# Strings are equal
return True
Case | How to Handle |
---|---|
Empty strings as input | Should return true if both strings are empty, false if only one is empty, and handle correctly the cases where backspaces delete all characters. |
Strings containing only backspaces | Should result in empty strings and be handled gracefully. |
Strings with leading or trailing backspaces | The solution should process leading and trailing backspaces correctly. |
Strings where backspaces delete across multiple characters | The algorithm must repeatedly apply the backspace until it reaches the beginning of the string or a non-backspace character. |
Very long strings to test for time complexity | The solution needs to efficiently process long strings, ideally in linear time. |
Strings with many consecutive backspaces | The algorithm needs to be able to correctly handle a large number of consecutive backspace characters without causing stack overflow or excessive computation. |
Strings with interleaved characters and backspaces | The solution should correctly process mixed sequences of characters and backspaces. |
Strings that result in an empty string after backspace operations | The algorithm should handle the case where the reduced string is empty after processing backspaces. |