856. Score of Parentheses

https://leetcode.com/problems/score-of-parentheses/

class Solution:
    def scoreOfParentheses(self, S: str) -> int:
        stack = []
        curr = 0
        for i in S:
            if i == "(":
                stack.append(curr)
                curr = 0
            else:
                curr = stack.pop() + max(1,curr*2)
        return curr

Last updated

Was this helpful?