-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path844.py
33 lines (27 loc) · 911 Bytes
/
844.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from typing import List
# Name: Backspace String Compare
# Link: https://leetcode.com/problems/backspace-string-compare/
# Method: Stack, construct end result
# Time: O(n)
# Space: O(n)
# Difficulty: Easy
class Solution:
@staticmethod
def text_editorize(raw_input: str) -> List[chr]:
buff_S = []
for c in raw_input:
if c == "#":
if buff_S: # Can't pop if no elems
buff_S.pop()
else:
buff_S.append(c)
return buff_S
def backspaceCompare(self, S: str, T: str) -> bool:
r1 = "".join(self.text_editorize(S))
r2 = "".join(self.text_editorize(T))
# print(r1 + " -- " + r2)
return r1 == r2
if __name__ == "__main__":
sol = Solution()
print(sol.backspaceCompare("a##c", "##a#c"))
print(sol.backspaceCompare("ab#c", "ad#c"))