Skip to content
Decoder

Score of Parentheses

Medium

Problem

Given a balanced string s of parentheses, compute its score with these rules:

- () scores 1. - AB (two balanced strings side by side) scores A + B. - (A) (a balanced string wrapped in brackets) scores 2 * A.

Example 1

Input
s = "()"
Output
1

Example 2

Input
s = "(())"
Output
2

Example 3

Input
s = "()()"
Output
2

Example 4

Input
s = "(()(()))"
Output
6
Why
2 * (1 + 2)

Constraints

  • 2 <= s.length <= 50
  • s contains only ( and ).
  • s is a balanced parentheses string.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.