Last active
April 10, 2020 16:23
-
-
Save yesidays/2cf69197918ec74686f01d9cef23764c to your computer and use it in GitHub Desktop.
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
class Solution(object): | |
def backspaceCompare(self, S, T): | |
""" | |
:type S: str | |
:type T: str | |
:rtype: bool | |
""" | |
def deleteBackspace(word): | |
temporal = [] | |
for w in word: | |
if w != '#': | |
temporal.append(w) | |
elif temporal: | |
temporal.pop() | |
return ''.join(temporal) | |
return deleteBackspace(S) == deleteBackspace(T) |
It is just a matter of look but I would replace lines 18
to 24
to be something like:
return deleteBackspace(S) == deleteBackspace(T)
def deleteBackspace(word):
temporal = []
for w in word:
if w != '#':
temporal.append(w)
elif temporal:
temporal.pop()
return ''.join(temporal)
return deleteBackspace(S) == deleteBackspace(T)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like the simplicity of the
deleteBackspace
method. Well done.