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 1
  • AB has score A + B, where A and B are balanced parentheses strings
  • (A) has score 2 * A, where A is 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
Score of Parentheses: Input → OutputInput(()(()))Apply Scoring RulesOutput6Rules:• () = 1• AB = A + B• (A) = 2 * ABreakdown:() at depth 1 → score 2() at depth 2 → score 4Total: 2 + 4 = 6
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
Asked in
Google 42 Microsoft 35
89.2K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen