Skip to content

Commit eae1871

Browse files
authored
Create check-if-word-is-valid-after-substitutions.py
1 parent eadf0e7 commit eae1871

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def isValid(self, S):
6+
"""
7+
:type S: str
8+
:rtype: bool
9+
"""
10+
stack = []
11+
for i in S:
12+
if i == 'c':
13+
if stack[-2:] == ['a', 'b']:
14+
stack.pop()
15+
stack.pop()
16+
else:
17+
return False
18+
else:
19+
stack.append(i)
20+
return not stack

0 commit comments

Comments
 (0)