-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode_0423.py
63 lines (57 loc) · 2.02 KB
/
lintcode_0423.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
57
58
59
60
61
62
63
# 423 · Valid Parentheses 423. 有效的括号序列
# https://www.lintcode.com/problem/423/
# Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
# The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
class Solution:
"""
@param s: A string
@return: whether the string is a valid parentheses
"""
def is_valid_parentheses(self, s: str) -> bool:
# write your code here
Outp=True
iSum=0
ParentVal={
'(':1,
')':-1,
'[':1,
']':-1,
'{':1,
'}':-1,
}
#for i in range(len(s)):
for ch in s:
#print(i,s[i],ParentVal.get(s[i]))
#iSum += ParentVal.get(s[i])
iSum += ParentVal.get(ch)
if ( iSum != 0 and iSum != 1):
Outp=False
break
if(iSum!=0):Outp=False
return Outp
def is_valid_parentheses2(self, s: str) -> bool:
# write your code here
stack=[]
for ch in s:
print(ch,stack)
if ch=='{' or ch=='[' or ch=='(':
stack.append(ch)
else:
if not stack:
return False
if ch==']' and stack[-1]!='[' or ch==')' and stack[-1]!='(' or ch=='}' and stack[-1]!='{':
return False
#print('stack[-1]:',stack[-1])
stack.pop()
return not stack
def main():
S=Solution()
#s='(){}[]{}}'; print(s,S.is_valid_parentheses(s))
#s='(){}[]{}'; print(s,S.is_valid_parentheses(s))
#s='(())'; print(s,S.is_valid_parentheses(s))
#s='('; print(s,S.is_valid_parentheses(s))
#s=')'; print(s,S.is_valid_parentheses(s))
#s='(){}[]{}}'; print(s,S.is_valid_parentheses2(s))
s='(()'; print(s,S.is_valid_parentheses2(s))
if __name__ == "__main__":
main()