-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20 Valid Parentheses.py
More file actions
44 lines (42 loc) · 1.09 KB
/
20 Valid Parentheses.py
File metadata and controls
44 lines (42 loc) · 1.09 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 19 16:47:40 2018
@author: yiqian
"""
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for i in xrange(len(s)):
if s[i]=="(":
stack.append("(")
if s[i]=="[":
stack.append("[")
if s[i]=="{":
stack.append("{")
if s[i]==")":
if len(stack)==0:
return False
temp = stack.pop()
if temp!="(" :
return False
if s[i]=="]":
if len(stack)==0:
return False
temp = stack.pop()
if temp!="[" :
return False
if s[i]=="}":
if len(stack)==0:
return False
temp = stack.pop()
if temp!="{" :
return False
if len(stack)!=0:
return False
else:
return True