856. Score of Parentheses (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution(object):
    def scoreOfParentheses(self, s):
        """
        :type s: str
        :rtype: int
        """
        # depth solution
        depth = 0
        result = 0
        for i, c in enumerate(s):
            if c == "(":
                depth += 1
            else:
                depth -= 1
                if s[i - 1] == "(":
                    result += 2 ** depth
        return result
        
        # stack solution
        stack = [0]
        for c in s:
            if c == "(":
                stack.append(0)
            else:
                m = stack.pop()
                stack[-1] += max(m * 2, 1)
        return stack[0]