Posted on 2023-10-20 04:09
Uriel 閱讀(45)
評論(0) 編輯 收藏 引用 所屬分類:
數據結構 、
閑來無事重切Leet Code
判斷兩個字符串是否相等,字符串中間如果出現#,則消除前一個字母
簡單棧操作
1 #844
2 #Runtime: 18 ms (Beats 31.98%)
3 #Memory: 13.3 MB (Beats 27.21%)
4
5 class Solution(object):
6 def backspaceCompare(self, s, t):
7 """
8 :type s: str
9 :type t: str
10 :rtype: bool
11 """
12 stk1 = []
13 stk2 = []
14 for ch in s:
15 if ch == '#':
16 if len(stk1):
17 stk1.pop()
18 else:
19 stk1.append(ch)
20 for ch in t:
21 if ch == '#':
22 if len(stk2):
23 stk2.pop()
24 else:
25 stk2.append(ch)
26 return stk1 == stk2