forked from kallaway/100-days-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020-07-28-valid-parentheses.py
56 lines (47 loc) · 1.55 KB
/
2020-07-28-valid-parentheses.py
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
46
47
48
49
50
51
52
53
54
55
56
# -------------------------------------------------------
# Valid Parentheses - https://leetcode.com/problems/valid-parentheses/
# -------------------------------------------------------
# Author: Arshad Mehmood
# Github: https://github.com/arshad115
# Blog: https://arshadmehmood.com
# LinkedIn: https://www.linkedin.com/in/arshadmehmood115
# Date : 2020-07-28
# Project: 100-days-of-leetcode
# -------------------------------------------------------
from typing import List
class Solution:
def isValid(self, s: str) -> bool:
#52 ms
if not s:
return True
if s.startswith((']', ')', '}')):
return False
while '()' in s or '[]' in s or '{}' in s:
s = s.replace("[]", "").replace("()", "").replace("{}", "")
if s:
return False
else:
return True
def isValidStack(self, s: str) -> bool:
# 40 ms
if not s:
return True
if s.startswith((']', ')', '}')):
return False
stack = []
for char in s:
if char == "(" or char == "{" or char == "[":
stack.append(char)
elif len(stack) <= 0:
return False
elif char == ")" and stack.pop() != "(":
return False
elif char == "]" and stack.pop() != "[":
return False
elif char == "}" and stack.pop() != "{":
return False
return not len(stack)
paran = "){[]}"
solution = Solution()
janein = solution.isValid(paran)
print(janein)