-
Notifications
You must be signed in to change notification settings - Fork 0
/
027.py
48 lines (36 loc) · 1.26 KB
/
027.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Problem:
Given a string of round, curly, and square open and closing brackets, return whether
the brackets are balanced (well-formed).
For example, given the string "([])", you should return true.
Given the string "([)]" or "((()", you should return false.
"""
from typing import Dict
from DataStructures.Stack import Stack
def is_parenthesis_balanced(
string: str, parenthesis_map: Dict[str, str] = {"{": "}", "[": "]", "(": ")"}
) -> bool:
open_parenthesis_set = set(parenthesis_map.keys())
stack = Stack()
# iterating through the string and checking if its balanced
for char in string:
if char in open_parenthesis_set:
stack.push(char)
elif not stack.is_empty() and parenthesis_map[stack.peek()] == char:
stack.pop()
else:
return False
# the string is balanced only if the stack is empty (equal number of opening and
# closing parenthesis)
return stack.is_empty()
if __name__ == "__main__":
print(is_parenthesis_balanced("([])"))
print(is_parenthesis_balanced("((([{}])))"))
print(is_parenthesis_balanced("([])[]({})"))
print(is_parenthesis_balanced("([)]"))
print(is_parenthesis_balanced("((()"))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""