This commit is contained in:
Martin Škovran 2026-10-13 14:10:13 +02:00
parent c8bb132585
commit 27305dd122

18
u6/matching_brackets.py Normal file
View File

@ -0,0 +1,18 @@
def is_paired(seq: str) -> bool:
stack = []
open_brackets = "([{"
close_brackets = ")]}"
for ch in seq:
if(ch in open_brackets):
stack.append(ch)
continue
matched_closed = close_brackets.find(ch)
if(matched_closed != -1):
if(not len(stack) or stack[-1] != open_brackets[matched_closed]):
return False
else:
stack.pop()
return not len(stack)