856. Score of Parentheses
Last updated
Last updated
class Solution:
def scoreOfParentheses(self, s: str) -> int:
stack = []
score = 0
for index, char in enumerate(list(s)):
if char == "(":
stack.append(score)
score = 0
else:
score = stack.pop() + max(score*2, 1)
return score
class Solution:
def scoreOfParentheses(self, s: str) -> int:
score = 0
par = 0
for index, char in enumerate(list(s)):
if char == "(":
par += 1
else:
par -= 1
if s[index-1] == "(":
score += (1<<par)
return score