Given two strings
T, return if they are equal when both are typed into empty text editors.
# means a backspace character.
Input: S = "ab#c", T = "ad#c" Output: true Explanation: Both S and T become "ac".
Input: S = "ab##", T = "c#d#" Output: true Explanation: Both S and T become "".
Input: S = "a##c", T = "#a#c" Output: true Explanation: Both S and T become "c".
Input: S = "a#c", T = "b" Output: false Explanation: S becomes "c" while T becomes "b".
1 <= S.length <= 200
1 <= T.length <= 200
Tonly contain lowercase letters and
- Can you solve it in
找到两个字符在遇到 # 就回退一个的操作后是否相同。
# Time: O(n) # Space: O(n) class Solution: def backspaceCompare(self, S: str, T: str) -> bool: s_stack, t_stack = ,  for s in S: if s == '#': if s_stack: s_stack.pop() else: s_stack.append(s) for t in T: if t == '#': if t_stack: t_stack.pop() else: t_stack.append(t) return s_stack == t_stack # 104/104 cases passed (32 ms) # Your runtime beats 37.24 % of python3 submissions # Your memory usage beats 100 % of python3 submissions (12.8 MB)