Advertisement
Guest User

Solution.py3

a guest
Apr 9th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. class Solution:
  2.     def backspaceCompare(self, S: str, T: str) -> bool:
  3.         def move(s, i):
  4.             while i >= 0 and s[i] == '#':
  5.                 c = 0
  6.                 while i >= 0 and s[i] == '#':
  7.                     c += 1
  8.                     i -= 1
  9.                 while i >= 0 and c > 0:
  10.                     c += 1 if s[i] == '#' else -1
  11.                     i -= 1
  12.  
  13.             return i
  14.  
  15.         i, j = len(S) - 1, len(T) - 1
  16.         while i >= 0 or j >= 0:
  17.             i = move(S, i)
  18.             j = move(T, j)
  19.             if i >= 0 and j >= 0:
  20.                 if S[i] != T[j]:
  21.                     return False
  22.                 i -= 1
  23.                 j -= 1
  24.             elif i >= 0 or j >= 0:
  25.                 return False
  26.  
  27.         return i == j
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement