summaryrefslogtreecommitdiff
path: root/sources/check_bracket.py
blob: bd7fa6e6205073ca91b1922bcb41629a7cbdae0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Uses python3
def check_bracket(exp):
    char_stack = []
    unmatched_opening_bracket = 0
    count = 0
    brackets = ['[', ']', '(', ')', '{', '}']
    for i in range(len(exp)):
        c = exp[i]
        if c not in brackets:
            count = count + 1
            continue
        if c == '[' or c == '(' or c == '{':
            char_stack.append(c)
            count = count + 1
            unmatched_opening_bracket = count
        else:
            if len(char_stack) == 0:
                return i + 1
            top = char_stack[-1]
            del (char_stack[-1])
            if (top == '[' and c != ']') or (top == '(' and c != ')') or (top == '{' and c != '}'):
                return i + 1
            else:
                unmatched_opening_bracket = unmatched_opening_bracket - 1
            count = count + 1
    if len(char_stack) == 0:
        return "Success"
    else:
        if unmatched_opening_bracket > 0:
            return unmatched_opening_bracket
    return count


exp = input()
print(check_bracket(exp))