-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_parentheses.py
More file actions
26 lines (20 loc) · 868 Bytes
/
Copy pathvalid_parentheses.py
File metadata and controls
26 lines (20 loc) · 868 Bytes
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
# -*- coding: utf-8 -*-
"""
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
solution:
Use stack to judge the characters,
(1)if the current char is the back one, then judge is it the corresponding one that last added in the stack
(2)if thr current char is the front one,add it to the stack
"""
class Solution(object):
def is_valid_parentheses(self, s):
pairs, stack = {'[': ']', '{': '}', '(': ')'}, []
for char in s:
if char in pairs:
stack.append(char)
elif len(stack) == 0 or pairs[stack.pop()] != char:
return False
return len(stack) == 0