aboutsummaryrefslogtreecommitdiff
path: root/backspace_string_compare.py
blob: 327ad2cdfccd50c07c876437bd04fb76e43152d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 844. Backspace String Compare
from functools import reduce

"""
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.
"""


class Solution(object):
    def back(self, res, c):
        if c != "#":
            res.append(c)
        elif res:
            res.pop()
        return res

    def backspaceCompare(self, s, t):
        return reduce(self.back, s, []) == reduce(self.back, t, [])


if __name__ == "__main__":
    obj = Solution()
    print(obj.backspaceCompare(s="ab#c", t="ad#c"))  # expect: 1
    print(obj.backspaceCompare(s="ab##", t="c#d#"))  # expect: 1
    print(obj.backspaceCompare(s="a#c", t="b"))  # expect: 0