-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_parenthesis.py
More file actions
57 lines (50 loc) · 2.1 KB
/
valid_parenthesis.py
File metadata and controls
57 lines (50 loc) · 2.1 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
45
46
47
48
49
50
51
52
53
54
55
56
57
""" Given a string 's" containing just the characters '(', ')', '{', '}', '[', and ']', determine if the input 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.
Every close bracket has a corresponding open bracket of the same type. """
def isvalid(s):
# false if string has only one character = False
if len(s) % 2 == 1:
return False
# starts with a closing character = False
if s[0] == ")" or s[0] == "}" or s[0] == "]":
return False
stack = []
# add opening brackets to stack, if closing bracket is found and corresponding opening bracket isn't found in stack
# return False
stack_peep = ""
for item in s:
if item == "(":
stack.append(item)
elif item == "{":
stack.append(item)
elif item == "[":
stack.append(item)
# if stack is empty and a closing bracket is used, return False
elif item == ")" and len(stack) == 0:
return False
elif item == "}" and len(stack) == 0:
return False
elif item == "]" and len(stack) == 0:
return False
elif item == ")" and len(stack) != 0:
stack_peep = stack.pop() # pop top item off stack to check if it is a corresponding bracket
if stack_peep != "(": # push back to stack if not corresponding
stack.append(stack_peep)
return False
elif item == "}" and len(stack) != 0:
stack_peep = stack.pop()
if stack_peep != "{":
stack.append(stack_peep)
return False
elif item == "]" and len(stack) != 0:
stack_peep = stack.pop()
if stack_peep != "[":
stack.append(stack_peep)
return False
# if stack cleared, means all corresponding brackets worked, not cleared means some unmatching brackets
if len(stack) == 0:
return True
else:
return False