856-Score-Of-Parentheses

Sat 17 May 2025

https://leetcode.com/problems/score-of-parentheses

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def scoreOfParentheses(self, S: str) -> int:
    ans = 0
    layer = 0

    for a, b in zip(S, S[1:]):
      if a + b == '()':
        ans += 1 << layer
      layer += 1 if a == '(' else -1

    return ans
new Solution().scoreOfParentheses()

Score: 5

Category: leetcode