<jupyter_start><jupyter_text>说明：    给定平衡括号内的字符串S，请根据以下规则计算字符串的分数：        1、 () has score 1        2、 AB has score A + B, where A and B are balanced parentheses strings.        3、 (A) has score 2 * A, where A is a balanced parentheses string.    Example 1:        Input: "()"        Output: 1    Example 2:        Input: "(())"        Output: 2    Example 3:        Input: "()()"        Output: 2    Example 4:        Input: "(()(()))"  --->(1 + 2) * 2        Output: 6    Note:        1、S is a balanced parentheses string, containing only ( and ).        2、2 <= S.length <= 50<jupyter_code>class Solution:
    def scoreOfParentheses(self, S: str) -> int:
        stack = []
        v_stack = []
        count = 0
        for i, s in enumerate(S):
            if s == '(':
                stack.append(i)
            else:
                past = stack.pop()
                if not stack:
                    if i - past > 1:
                        count += sum(v_stack) * 2
                    else:
                        v_stack.append(1)
                        count += sum(v_stack)
                    v_stack.clear()
                elif i - past > 1:
                    val = sum(v_stack) * 2
                    v_stack.clear()
                    v_stack.append(val)
                else:
                    v_stack.append(1)
        return count
class Solution:
    def scoreOfParentheses(self, S: str) -> int:
        count, flag, stack = 0, 0, []
        for i in range(len(S)):
            if S[i] == '(':
                flag = 1
                stack.append('(')
            else:
                if flag == 1:
                    count += 2 ** (len(stack) - 1)
                    flag = 0
                stack.pop()
        return count
class Solution:
    def scoreOfParentheses(self, S: str) -> int:
        stack, count = [], 0
        for s in S:
            if s == '(':
                stack.append(s)
            else:
                score = 0
                while stack[-1] != '(':
                    score += stack.pop()
                stack.pop()
                if score == 0:
                    stack.append(1)
                else:
                    stack.append(score * 2)
        return sum(stack)
solution = Solution()
solution.scoreOfParentheses("(()(()))")
"(4)"
1 +　1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12030716723549488, 'fraction_numerical': 0.01877133105802048, 'mean_word_length': 1.1126126126126126, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '618102', 'license': 'Apache-2.0', 'n_tokens_mistral': 735, 'n_tokens_neox': 669, 'n_words': 192, 'path': 'String/1014/856. Score of Parentheses.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '2344'}