Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 508 Bytes

856.md

File metadata and controls

23 lines (21 loc) · 508 Bytes

856. Score of Parentheses

Solution 1 (time O(n), space O(n))

class Solution(object):
    def scoreOfParentheses(self, s):
        """
        :type s: str
        :rtype: int
        """
        st = [0]
        for c in s:
            if c == "(":
                st.append(0)
            else:
                t = st.pop()
                if t == 0:
                    st[-1] += 1
                else:
                    st[-1] += 2 * t
        return st[-1]