-
Notifications
You must be signed in to change notification settings - Fork 0
/
slip10B.py
32 lines (27 loc) · 1.18 KB
/
slip10B.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
class BracketValidator:
def __init__(self):
self.opening_brackets = "({["
self.closing_brackets = ")}]"
self.stack = []
def is_valid(self, s):
for bracket in s:
if bracket in self.opening_brackets:
self.stack.append(bracket)
elif bracket in self.closing_brackets:
if not self.stack:
return False # No corresponding opening bracket found
top = self.stack.pop()
if not self.is_matching(top, bracket):
return False # Mismatched opening and closing brackets
return not self.stack # Stack should be empty at the end
def is_matching(self, open_bracket, close_bracket):
return self.opening_brackets.index(open_bracket) == self.closing_brackets.index(close_bracket)
# Example usage
validator = BracketValidator()
# Test cases
valid_cases = ["()", "()[]{}", "{[()]}", "({[{}]})"]
invalid_cases = ["[)", "({[)]", "{{{"]
for case in valid_cases:
print(f'"{case}" is valid: {validator.is_valid(case)}')
for case in invalid_cases:
print(f'"{case}" is valid: {validator.is_valid(case)}')