 All Problems
Score of Parentheses
medium
stack
string
amazon
google

Given a balanced parentheses string s, return the score of the string. The score is based on these rules:

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

Example 1:

Input: ()
Output: 1

Example 2:

Input: (())
Output: 2

Example 3:

Input: ()()
Output: 2

Example 4:

Input: (()(()))
Output: 6

Constraints:

  • 2 ≤ s.length ≤ 50
  • s consists of '(' and ')'
  • s is a balanced parentheses string

Input format: One line — balanced parentheses string.

Output format: Score.

Run to check your code against the sample cases, or submit to run every case