python stack 844. Backspace String Compare

leetcode 844 字符串匹配,遇到‘#’回退

思想:使用栈,遍历字符串,若不为‘#’,压栈,如果栈不空,出栈,如果空,继续遍历

class Solution(object):
    def backspaceCompare(self, S, T):
        """
        :type S: str
        :type T: str
        :rtype: bool
        """
        stack1=[]
        stack2=[]
        for s1 in S:
            if s1!='#':
                stack1.append(s1)
            elif stack1:
                stack1.pop()
            else:
                continue
        for s2 in T:
            if s2!='#':
                stack2.append(s2)
            elif stack2:
                stack2.pop()
            else:
                continue
        if stack1==stack2:
            return True
        else:
            return False

你可能感兴趣的:(python,python)