diff options
Diffstat (limited to 'backspace_string_compare.py')
-rw-r--r-- | backspace_string_compare.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/backspace_string_compare.py b/backspace_string_compare.py new file mode 100644 index 0000000..327ad2c --- /dev/null +++ b/backspace_string_compare.py @@ -0,0 +1,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 |