Skip to main content
Back to problems
Leetcode
Medium
Stacks
Recursion
Math
Google
Meta
Score of Parentheses

Compute the score of a balanced parentheses string using the rule that () = 1 and concatenation/nesting combine scores.

Acceptance 0%
Problem Statement

Score of Parentheses

You are given a balanced parentheses string s consisting only of '(' and ')'.

Define the score of a balanced string as follows:

  • "()" has score 1
  • AB has score score(A) + score(B), where A and B are balanced strings
  • (A) has score 2 * score(A), where A is a balanced string

Return the score of s.

A string is balanced if it is empty or can be built from the rules above.

Input Format

  • A single string s
  • s contains only '(' and ')'
  • s is guaranteed to be balanced

Output Format

  • Return one integer: the score of the parentheses string

Constraints

  • The string is balanced
  • Length is at least 2
  • 1 <= |s| <= 50 in typical interview settings; exact platform limits may differ
Examples
Sample cases returned by the problem API.

Example 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.

Show 1 more example

Example 3

Input

s = "()()"

Output

2

Explanation

The string is a concatenation of two balanced parts: 1 + 1 = 2.

Premium problem context

Unlock deeper context for this problem

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.