856. Score of Parentheses
Keep Track the level , when it pop up a level. current * 2
class Solution:
def scoreOfParentheses(self, s: str) -> int:
arr = []
def cal(sss):
stack = []
currentLevel = 0
current = defaultdict(int)
for s in sss:
if s == '(':
currentLevel += 1
stack.append(currentLevel)
else:
if current[currentLevel+1]:
current[currentLevel] += 2 * current[currentLevel+1]
current[currentLevel+1] = 0
else:
current[currentLevel] += 1
currentLevel -= 1
stack.pop()
return current[1]
# l = 0
# current = ''
# for ss in s:
# current = current + ss
# if ss == "(":
# l += 1
# else:
# l -= 1
# if l == 0:
# arr.append(current)
# current = ''
# # ((()()))
# res = 0
# for sss in arr:
# res += cal(sss)
return cal(s)