856. Score of Parentheses
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 19, 2024
Last updated : June 19, 2024
Related Topics : String, Stack
Acceptance Rate : 63.87 %
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]