Compute the score of a balanced parentheses string using the rule that () = 1 and concatenation/nesting combine scores.
You are given a balanced parentheses string s consisting only of '(' and ')'.
Define the score of a balanced string as follows:
"()" has score 1AB has score score(A) + score(B), where A and B are balanced strings(A) has score 2 * score(A), where A is a balanced stringReturn the score of s.
A string is balanced if it is empty or can be built from the rules above.
ss contains only '(' and ')'s is guaranteed to be balanced1 <= |s| <= 50 in typical interview settings; exact platform limits may differExample 1
Input
s = "()"
Output
1
Explanation
The smallest balanced pair has score 1.
Example 2
Input
s = "(())"
Output
2
Explanation
The inner "()" scores 1, and wrapping it in parentheses doubles the score to 2.
Example 3
Input
s = "()()"
Output
2
Explanation
The string is a concatenation of two balanced parts: 1 + 1 = 2.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.