forked from fspv/learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackspace-string-compare.py
77 lines (52 loc) · 2.06 KB
/
backspace-string-compare.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import unittest
class Solution:
def real_value(self, string, pos):
if pos < 0 or string[pos] != "#":
return pos
backspace_count = 0
while string[pos] == "#":
backspace_count += 1
pos -= 1
for _ in range(backspace_count):
pos = self.real_value(string, pos - 1)
return pos
def backspaceCompare(self, S, T):
S_ptr = len(S) - 1
T_ptr = len(T) - 1
while S_ptr >= 0 or T_ptr >= 0:
S_ptr, T_ptr = self.real_value(S, S_ptr), self.real_value(T, T_ptr)
if S_ptr >= 0 and T_ptr >= 0:
if S[S_ptr] != T[T_ptr]:
return False
elif S_ptr >= 0 or T_ptr >= 0:
return False
S_ptr -= 1
T_ptr -= 1
return True
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_empty1(self):
self.assertEqual(self.sol.backspaceCompare("", ""), True)
def test_empty2(self):
self.assertEqual(self.sol.backspaceCompare("", "a#"), True)
def test_empty3(self):
self.assertEqual(self.sol.backspaceCompare("a#", "a#"), True)
def test_empty4(self):
self.assertEqual(self.sol.backspaceCompare("a#", ""), True)
def test_empty5(self):
self.assertEqual(self.sol.backspaceCompare("a#", "a"), False)
def test_empty6(self):
self.assertEqual(self.sol.backspaceCompare("ab##", "c#d#"), True)
def test_custom1(self):
self.assertEqual(self.sol.backspaceCompare("a##c", "#a#c"), True)
def test_custom2(self):
self.assertEqual(self.sol.backspaceCompare("a#c", "b"), False)
def test_custom3(self):
self.assertEqual(self.sol.backspaceCompare("abcdef#gh", "abcdef#f#f#f#ghvg##"), True)
def test_custom4(self):
self.assertEqual(self.sol.backspaceCompare("bxj##tw", "bxj###tw"), False)
def test_custom5(self):
self.assertEqual(self.sol.backspaceCompare("bbbextm", "bbb#extm"), False)
if __name__ == "__main__":
unittest.main()