Skip to content

Latest commit

 

History

History
62 lines (47 loc) · 1.36 KB

_856. Score of Parentheses.md

File metadata and controls

62 lines (47 loc) · 1.36 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 19, 2024

Last updated : June 19, 2024


Related Topics : String, Stack

Acceptance Rate : 63.87 %


Solutions

Python

class Solution:
    def scoreOfParentheses(self, s: str) -> int:
        stk = [0]

        for c in s :
            if c == '(' :
                stk.append(0)
                continue
            popped = max(1, 2 * stk.pop())
            stk[-1] += popped

        return stk[0]
class Solution:
    def scoreOfParentheses(self, s: str) -> int:
        stk = [0]

        for c in s :
            if c == '(' :
                stk.append(0)
            else :
                temp = stk.pop()
                if temp == 0 :
                    temp = 1
                else :
                    temp *= 2
                stk[-1] = stk[-1] + temp
        
        print(stk)
        return stk[-1]