mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
Merge pull request #180 from mathildapurr/fix_balanced_brackets
fixed issue #171
This commit is contained in:
commit
b039fcb4ce
|
@ -18,28 +18,20 @@ returns true if S is nested and false otherwise.
|
|||
def is_balanced(S):
|
||||
|
||||
stack = []
|
||||
open_brackets = set({'(', '[', '{'})
|
||||
closed_brackets = set({')', ']', '}'})
|
||||
open_to_closed = dict({'{':'}', '[':']', '(':')'})
|
||||
|
||||
for i in range(len(S)):
|
||||
|
||||
if S[i] == '(' or S[i] == '{' or S[i] == '[':
|
||||
if S[i] in open_brackets:
|
||||
stack.append(S[i])
|
||||
|
||||
else:
|
||||
|
||||
if len(stack) > 0:
|
||||
|
||||
pair = stack.pop() + S[i]
|
||||
|
||||
if pair != '[]' and pair != '()' and pair != '{}':
|
||||
return False
|
||||
|
||||
else:
|
||||
elif S[i] in closed_brackets:
|
||||
if len(stack) == 0 or (len(stack) > 0 and open_to_closed[stack.pop()] != S[i]):
|
||||
return False
|
||||
|
||||
if len(stack) == 0:
|
||||
return True
|
||||
|
||||
return False
|
||||
return len(stack) == 0
|
||||
|
||||
|
||||
def main():
|
||||
|
|
Loading…
Reference in New Issue
Block a user