class Solution:
def scoreOfParentheses(self, s: str) -> int:
stack = [0]
for char in s:
if char == '(':
stack.append(0)
else:
v = stack.pop()
stack[-1] += max(2 * v, 1)
return stack[0]
class Solution {
public:
int scoreOfParentheses(string s) {
stack<int> st;
st.push(0);
for (char c : s) {
if (c == '(') {
st.push(0);
} else {
int v = st.top(); st.pop();
int w = st.top(); st.pop();
st.push(w + max(2 * v, 1));
}
}
return st.top();
}
};
class Solution {
public int scoreOfParentheses(String s) {
Deque<Integer> stack = new ArrayDeque<>();
stack.push(0);
for (char c : s.toCharArray()) {
if (c == '(') {
stack.push(0);
} else {
int v = stack.pop();
int w = stack.pop();
stack.push(w + Math.max(2 * v, 1));
}
}
return stack.peek();
}
}
var scoreOfParentheses = function(s) {
let stack = [0];
for (let c of s) {
if (c === '(') {
stack.push(0);
} else {
let v = stack.pop();
stack[stack.length - 1] += Math.max(2 * v, 1);
}
}
return stack[0];
};
Given a balanced parentheses string s
, compute its score based on the following rules:
()
has score 1AB
has score A + B
(where A
and B
are balanced parentheses strings)(A)
has score 2 * A
(where A
is a balanced parentheses string)
You are given a string s
consisting only of '(' and ')' characters, and s
is guaranteed to be a valid balanced parentheses string. Your task is to return the score of s
according to the rules above.
Constraints:
len(s)
≤ 50s
is a balanced parentheses stringAt first glance, this problem seems like it requires parsing and recursively evaluating the structure of the parentheses. If we try to brute-force, we might attempt to split the string at every level, recursively score the substrings, and combine the results. However, this would be inefficient and complex.
The key insight is to recognize that the score depends on the structure of the parentheses, not on their specific positions. The rules are recursive and compositional, so we can process the string in one pass, keeping track of the current "depth" or using a stack to manage nested expressions.
Instead of recursively splitting, we can use a stack to simulate the process, adding scores as we close parentheses, and doubling scores when we encounter nested pairs. This approach is both efficient and elegant.
This approach works because the stack naturally mirrors the nested structure of the parentheses, and we always combine scores according to the problem's rules as we unwind the stack.
v
. If v == 0
, add 1 to the new top of the stack. Otherwise, add 2 * v
to the new top.
Let's walk through the input s = "(()(()))"
step by step:
The final score is 6.
This shows how the stack accumulates scores as we close parentheses, handling both simple and nested cases.
The "Score of Parentheses" problem can be solved efficiently using a stack to track the score at each level of nesting. By simulating the process of evaluating the parentheses structure, we avoid the complexity of recursion and achieve a linear-time solution. The key insight is to interpret the problem's rules in terms of stack operations, making the solution both concise and elegant.