Score of Parentheses - Problem
Given a balanced parentheses string s, return the score of the string.
The score of a balanced parentheses string is based on the following rules:
()has score 1ABhas scoreA + B, whereAandBare balanced parentheses strings(A)has score2 * A, whereAis a balanced parentheses string
Input & Output
Example 1 — Basic Nested
$
Input:
s = "(())"
›
Output:
2
💡 Note:
Inner () has score 1, outer parentheses double it: 2 * 1 = 2
Example 2 — Complex Structure
$
Input:
s = "(()(()))"
›
Output:
6
💡 Note:
First () at depth 1 contributes 2¹ = 2, second () at depth 2 contributes 2² = 4, total: 2 + 4 = 6
Example 3 — Simple Concatenation
$
Input:
s = "()()"
›
Output:
2
💡 Note:
Two separate () pairs: 1 + 1 = 2
Constraints
- 2 ≤ s.length ≤ 50
- s consists of only '(' and ')'
Visualization
Tap to expand
Understanding the Visualization
1
Input
Balanced parentheses string
2
Process
Apply scoring rules based on structure
3
Output
Numerical score
Key Takeaway
🎯 Key Insight: Only () pairs contribute to score, each contributing 2^depth where depth is the nesting level
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code