Score a Balanced Parenthesis Expression

Quick Overview

Score a Balanced Parenthesis Expression. For balanced s, score "()" as 1, concatenation AB as score(A)+score(B), and nesting (A) as 2*score(A).

Score a Balanced Parenthesis Expression

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem For balanced s, score "()" as 1, concatenation AB as score(A)+score(B), and nesting (A) as 2*score(A). Return the total score. ```cpp class Solution { public: int scoreOfParentheses(string s); }; ``` ### Examples **Example 1:** ```text s = "(()())" Output: 4 Explanation: The inner concatenation scores2, then the outer pair doubles it. ``` **Example 2:** ```text s = "()(())" Output: 3 Explanation: The two concatenated pieces score1 and2. ``` ### Constraints - Input length lies in [2,50]. - Characters are parentheses only and the string is balanced. ### Contract considerations Keep additive concatenation and multiplicative nesting distinct. ```hint Read the boundary condition How many enclosing pairs double the contribution of one innermost ()? ``` ### Boundary reminders Keep additive concatenation and multiplicative nesting distinct.

Overview: Score a Balanced Parenthesis Expression. For balanced s, score "()" as 1, concatenation AB as score(A)+score(B), and nesting (A) as 2*score(A).

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

For balanced s, score "()" as 1, concatenation AB as score(A)+score(B), and nesting (A) as 2*score(A). Return the total score.

class Solution {
public:
    int scoreOfParentheses(string s);
};

Examples

Example 1:

s = "(()())"
Output: 4
Explanation: The inner concatenation scores2, then the outer pair doubles it.

Example 2:

s = "()(())"
Output: 3
Explanation: The two concatenated pieces score1 and2.

Constraints

  • Input length lies in [2,50].
  • Characters are parentheses only and the string is balanced.

Contract considerations

Keep additive concatenation and multiplicative nesting distinct.

Boundary reminders

Keep additive concatenation and multiplicative nesting distinct.