Skip to content
the poor coder

Leetcode - Backspace String Compare Solution

Poor Man
Published
Updated
Reading time
1 min read

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:

Text
Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".

Example 2:

Text
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".

Example 3:

Text
Input: s = "a##c", t = "#a#c"
Output: true
Explanation: Both s and t become "c".

Example 4:

Text
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?

Solution in Python#

Python
class Solution:
    @staticmethod
    def build(keys):
        stack = []
        for i in keys:
            if i=="#":
                if stack:
                    stack.pop()
            else:
                stack.append(i)
        return stack
    def backspaceCompare(self, S: str, T: str) -> bool:
        return self.build(T)==self.build(S)

Keep going

2021

Leetcode - Crawler Log Folder Solution

The Leetcode file system keeps a log each time some user performs a change folder operation. The operations are described below: "../" : Move to the parent folder of the current folder. (If you are already in the main…

leetcodestack1 min read
2021

Leetcode - Make The String Great Solution

Given a string s of lower and upper case English letters. A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where: 0 <= i <= s.length - 2 s[i] is a lower-case letter and s[i + 1] is…

ESC