Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
883 B
1
Indexable
Never
def check_braces(data):
    stack = []
    for c in data:
        if c == '{':
            stack.append(c)
        if c == '}':
            if not stack: return False  # First case: too many closing
            stack.pop()
    return not stack  # Second case too many opening


def foo(s):
    sb = ''
    open = 0
    for c in reversed(s):
        if c == '}':
            open += 1
        elif c == '{':
            if open == 0:
                continue
            open -= 1
        sb += c
    result = ''
    for i in range(0, len(sb), 1):
        if sb[i] == '}':
            open -= 1
            if open >= 0:
                continue
        result += sb[i]
    return list(reversed(result))

inp = input()
if (check_braces(inp)):
    print('-1')
else:
    t = foo(inp)
    for i in range(len(inp)):
        if (inp[i] != t[i]):
            print(i - 1)
            break