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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code