İç İçe Parantezler

is_pair = lambda l, r: {"{": "}", "[": "]", "(": ")"}[l] == r
check = lambda s: (check(s[2:]) if is_pair(s[0], s[1]) else is_pair(s[0], s[-1]) and check(s[1:-1])) if s else True
solution = lambda s: len(s)%2 == 0 and check(s)

assert solution("") == True
assert solution("[(){}]") == True
assert solution("()()") == True
assert solution("[(])") == False
assert solution("(") == False

Kod hatalı.

2 Beğeni