MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / is_balanced

Function is_balanced

other/nested_brackets.py:19–35  ·  view source on GitHub ↗
(S)

Source from the content-addressed store, hash-verified

17
18
19def is_balanced(S):
20
21 stack = []
22 open_brackets = set({'(', '[', '{'})
23 closed_brackets = set({')', ']', '}'})
24 open_to_closed = dict({'{':'}', '[':']', '(':')'})
25
26 for i in range(len(S)):
27
28 if S[i] in open_brackets:
29 stack.append(S[i])
30
31 elif S[i] in closed_brackets:
32 if len(stack) == 0 or (len(stack) > 0 and open_to_closed[stack.pop()] != S[i]):
33 return False
34
35 return len(stack) == 0
36
37
38def main():

Callers 1

mainFunction · 0.85

Calls 1

popMethod · 0.45

Tested by

no test coverage detected