forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackspace String Compare.py
More file actions
45 lines (41 loc) · 1.25 KB
/
Backspace String Compare.py
File metadata and controls
45 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Backspace String Compare
# https://leetcode.com/problems/backspace-string-compare/
class Solution(object):
def getFinalString(self, string):
stack = []
for char in string:
if(char == '#'):
if stack:
stack.pop()
else:
stack.append(char)
return stack
def backspaceCompare(self, S, T):
return self.getFinalString(S) == self.getFinalString(T)
"""
:type S: str
:type T: str
:rtype: bool
"""
class LeetcodeSuggested(object):
def getFinalString(self, string):
skipCount = 0
stng = ""
for char in reversed(string):
if(char == '#'):
skipCount = skipCount+1
else:
if(skipCount > 0):
skipCount = skipCount -1
else:
stng = stng + char
return stng[::-1]
def backspaceCompare(self, S, T):
return self.getFinalString(S) == self.getFinalString(T)
# Main
obj = Solution()
obj = LeetcodeSuggested()
print obj.backspaceCompare("ab#c", "ab#c")
obj = Solution()
obj = LeetcodeSuggested()
print obj.backspaceCompare("a#c", "b")