LeetCode 844 Backspace String Compare (Python)

Posted by 小明MaxMing on April 26, 2020

题目

Given two strings S and T, return 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.

解题思路

代码

class Solution:
    def backspaceCompare(self, S: str, T: str) -> bool:
        def build(s):
            s = list(s)
            i = 0
            for c in s:
                if c != '#':
                    s[i] = c
                    i += 1
                else:
                    if i:
                        i -= 1
            return ''.join(s[:i])
        return build(S) == build(T)

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅