-
Notifications
You must be signed in to change notification settings - Fork 6
/
backspace_compare_strings.py
42 lines (34 loc) · 1.08 KB
/
backspace_compare_strings.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
class Solution:
def backspaceCompare(self, str1: str, str2: str) -> bool:
ptr1, ptr2 = len(str1) - 1, len(str2) - 1
while ptr1 >= 0 or ptr2 >= 0:
skipA, skipB = 0, 0
while ptr1 >= 0:
# counting a character two times // decrementing the pointer two times for every # that is encountered
if str1[ptr1] == '#':
skipA += 1
ptr1 -= 1
elif skipA > 0:
skipA -= 1
ptr1 -=1
# for a non-hash char when the skip is 0(no # encountered so far) - compare chars
else:
print("skipA", skipA, ptr1)
break
while ptr2 >= 0:
if str2[ptr2] == '#':
skipB += 1
ptr2 -= 1
elif skipB > 0:
skipB -= 1
ptr2 -= 1
else:
print("skipB", skipB, ptr2)
break
if (ptr1 >= 0) != (ptr2 >= 0):
return False
if ptr1 >= 0 and ptr2 >= 0 and str1[ptr1] != str2[ptr2]:
return False
ptr1 -= 1
ptr2 -= 1
return True