-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
25 lines (23 loc) · 827 Bytes
/
Copy pathstack.py
File metadata and controls
25 lines (23 loc) · 827 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
# Valid Paretheses
def is_valid_parentheses(s):
# Initialize a stack to keep track of opening parentheses
stack = []
# Create a mapping of closing parentheses to their corresponding opening parentheses
mapping = {
')': '(',
'}': '{',
']': '['
}
# Loop through each character in the string
for char in s:
if char in mapping:
# If it's a closing parenthesis, check if the top element of the stack matches the corresponding opening parenthesis
top_element = stack.pop() if stack else '#'
if mapping[char] != top_element:
return False
else:
# If it's an opening parenthesis, push it onto the stack
stack.append(char)
return not stack
s = "([{}])"
print(is_valid_parentheses(s))