Skip to content

Instantly share code, notes, and snippets.

@jovianlin
Last active December 11, 2018 05:10
Show Gist options
  • Save jovianlin/36e4c15e69c17df4b4f7a1221c9ccd28 to your computer and use it in GitHub Desktop.
Save jovianlin/36e4c15e69c17df4b4f7a1221c9ccd28 to your computer and use it in GitHub Desktop.
class Solution:
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
pointerS, pointerT = len(S)-1, len(T)-1
skipS, skipT = 0, 0
while pointerS >= 0 or pointerT >= 0:
while pointerS >= 0 and S[pointerS] == '#':
skipS += 1
pointerS -= 1
while pointerS >= 0 and skipS > 0 and S[pointerS] != '#':
pointerS -= 1
skipS -= 1
while pointerT >= 0 and T[pointerT] == '#':
skipT += 1
pointerT -= 1
while pointerT >= 0 and skipT > 0 and T[pointerT] != '#':
pointerT -= 1
skipT -= 1
if pointerS >= 0 or pointerT >= 0:
if S[pointerS] == T[pointerT]:
pointerS -= 1
pointerT -= 1
else:
return False
# End of while loop
if pointerS == pointerT:
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment