Backspace String Compare

Given two strings S andT, return if they are equal when both are typed into empty text editors.#means a backspace character.

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 = 
"#a#c"
Output: 
true

Explanation
: Both S and T become "c".

Example 4:

Input: 
S = 
"a#c"
, T = 
"b"
Output: 
false

Explanation
: S becomes "c" while T becomes "b".

Note:

1 <= S.length <= 200
1 <= T.length <= 200
S and T only contain lowercase letters and '#' characters.

分析

栈内只加字母,不加#,遇到‘#’,抵消栈内char,字母的话加入。 最后比较S,T剩下的东西

class Solution:
    def backspaceCompare(self, S: str, T: str) -> bool:
        return self.getChar(S) == self.getChar(T)

    def getChar(self, S: str) -> str:
        stack = []
        for i in S:
            if stack and i == '#':
                stack.pop()
            elif i.isalpha():
                stack.append(i)
        return ''.join(stack)

Last updated