Score of Parentheses

Medium

Topics
StringStack

Given a balanced parentheses string s, compute its score: () is 1, AB is A+B, and (A) is 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

Constraints

  • 2 <= s.length <= 50
  • s is a balanced parentheses string.
Run ⌘' · Submit ⌘⏎