Created
April 10, 2020 05:03
-
-
Save munguial/361a9b8fb986b1e4a06cbd2add414ba4 to your computer and use it in GitHub Desktop.
Day 9 - Backspace String Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3291/ | |
class Solution: | |
def backspaceCompare(self, S: str, T: str) -> bool: | |
a = len(S) - 1 | |
b = len(T) - 1 | |
skipA = 0 | |
skipB = 0 | |
while a >= 0 or b >= 0: | |
if a >= 0 and S[a] == '#': | |
skipA += 1 | |
a -= 1 | |
continue | |
if b >= 0 and T[b] == '#': | |
skipB += 1 | |
b -= 1 | |
continue | |
if skipA: | |
skipA -= 1 | |
a -= 1 | |
continue | |
if skipB: | |
skipB -= 1 | |
b -= 1 | |
continue | |
if a < 0 or b < 0 or S[a] != T[b]: | |
return False | |
a -= 1 | |
b -= 1 | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment