leetcode844 Backspace String Compare

 1 """
 2 Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
 3 Example 1:
 4 Input: S = "ab#c", T = "ad#c"
 5 Output: true
 6 Explanation: Both S and T become "ac".
 7 Example 2:
 8 Input: S = "ab##", T = "c#d#"
 9 Output: true
10 Explanation: Both S and T become "".
11 Example 3:
12 Input: S = "a##c", T = "#a#c"
13 Output: true
14 Explanation: Both S and T become "c".
15 Example 4:
16 Input: S = "a#c", T = "b"
17 Output: false
18 Explanation: S becomes "c" while T becomes "b".
19 """
20 """
21 简单题通过
22 """
23 class Solution:
24     def backspaceCompare(self, S, T):
25         """
26         :type S: str
27         :type T: str
28         :rtype: bool
29         """
30         return self._back(S) == self._back(T)
31     def _back(self, string):
32         stack = []
33         for i in range(len(string)):
34             if string[i] == '#':
35                 if stack: #这里重要,需要判断stack不为空,对于这种[#a#c]输入
36                     stack.pop()
37                 continue
38             stack.append(string[i])
39         return stack

猜你喜欢

转载自www.cnblogs.com/yawenw/p/12343498.html