【Leetcode每日一题】844. 比较含退格的字符串(栈)

Leetcode 每日一题
题目链接:844. 比较含退格的字符串
解题思路:用栈来解决,将两个字符串S,T处理后比较,注意栈为空时不能pop。
题解:

class Solution:
    def backspaceCompare(self, S: str, T: str) -> bool:
        s1 = self.solve(S)
        t1 = self.solve(T)
        return s1 == t1
    
    def solve(self, string: str):
        stack = []
        for example in string:
            if example == '#':
                if len(stack) > 0:
                    stack.pop()
            else:
                stack.append(example)
        return stack

猜你喜欢

转载自blog.csdn.net/qq_37753409/article/details/109156037