Skip to content
Snippets Groups Projects
Select Git revision
  • 2a701f3c48e07bd53d7b5451940f3bf7ab7519c3
  • master default protected
2 results

cse16218_ex2_10.py

Blame
  • cse16218_ex2_10.py 509 B
    def depth(s, n): 
        current_max = 0
        final_max = 0
        
        for i in range(n): 
            if s[i] == '(': 
                current_max += 1
      
                if current_max > final_max: 
                    final_max = current_max 
            elif s[i] == ')': 
                if current_max > 0: 
                    current_max -= 1
                else: #unbalanced
                    return -1
                    
        if current_max != 0: #unbalanced
            return -1
      
        return final_max
      
    s = input()
    print (depth(s, len(s)))