File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def backspaceCompare (self , str1 : str , str2 : str ) -> bool :
3+
4+ ptr1 , ptr2 = len (str1 ) - 1 , len (str2 ) - 1
5+
6+ while ptr1 >= 0 or ptr2 >= 0 :
7+ skipA , skipB = 0 , 0
8+
9+ while ptr1 >= 0 :
10+ # counting a character two times // decrementing the pointer two times for every # that is encountered
11+ if str1 [ptr1 ] == '#' :
12+ skipA += 1
13+ ptr1 -= 1
14+ elif skipA > 0 :
15+ skipA -= 1
16+ ptr1 -= 1
17+ # for a non-hash char when the skip is 0(no # encountered so far) - compare chars
18+ else :
19+ print ("skipA" , skipA , ptr1 )
20+ break
21+
22+ while ptr2 >= 0 :
23+ if str2 [ptr2 ] == '#' :
24+ skipB += 1
25+ ptr2 -= 1
26+ elif skipB > 0 :
27+ skipB -= 1
28+ ptr2 -= 1
29+ else :
30+ print ("skipB" , skipB , ptr2 )
31+ break
32+
33+ if (ptr1 >= 0 ) != (ptr2 >= 0 ):
34+ return False
35+
36+ if ptr1 >= 0 and ptr2 >= 0 and str1 [ptr1 ] != str2 [ptr2 ]:
37+ return False
38+
39+ ptr1 -= 1
40+ ptr2 -= 1
41+
42+ return True
You can’t perform that action at this time.
0 commit comments