Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
Example
Input: S = “ab#c”, T = “ad#c”
Output: true
Explanation: Both S and T become “ac”.
Solution
from functools import reduce
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
def back(res, c):
if c!='#':
res.append(c)
elif res:
res.pop()
return res
return reduce(back, S, []) == reduce(back, T, [])