-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.ValidParenthesis.py
More file actions
50 lines (41 loc) · 1.56 KB
/
Copy path20.ValidParenthesis.py
File metadata and controls
50 lines (41 loc) · 1.56 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
# Author: Madhu Kumar K S
# Valid Parenthesis Checker
# This program checks if a string of parentheses is valid using a stack data structure
def valid_parenthesis(s: str) -> bool:
"""
Check if the given string has valid parentheses arrangement.
Args:
s (str): String containing parentheses characters
Returns:
bool: True if parentheses are valid, False otherwise
"""
# Initialize empty stack to keep track of opening brackets
stack = []
# Dictionary mapping closing brackets to their corresponding opening brackets
pairs = {
')': '(',
']': '[',
'}': '{'
}
# Iterate through each character in the input string
for char in s:
# If character is a closing bracket
if char in pairs:
# Pop from stack if not empty, otherwise use placeholder "#"
top = stack.pop() if stack else "#"
# Check if the popped opening bracket matches the current closing bracket
if pairs[char] != top:
return False
else:
# If character is an opening bracket, push it onto the stack
stack.append(char)
# Return True if stack is empty (all brackets were matched), False otherwise
return len(stack) == 0
# Test the function with sample input
input_parenthesis = "({[]})"
result = valid_parenthesis(input_parenthesis)
# Display the result based on validation outcome
if result is True:
print(f"Given input has valid parenthesis")
else:
print(f"Given input is not valid parenthesis")