Ctrl K

Worth of Nested Brackets

Medium
You get a balanced string brackets made of ( and ). Its worth is defined as follows: - an empty pair () is worth 1; - two balanced strings written one after another are worth the sum of their worths; - a balanced string wrapped in one more pair of brackets is worth twice as much as without the wrapper. Return the worth of brackets.
Example 1
brackets="()(())" → 3

() is worth 1, (()) is worth 2 · 1 = 2, together 1 + 2 = 3.

Example 2
brackets="((())())" → 6

Inside the outer pair sit (()) = 2 and () = 1, so the total is 2 · (2 + 1) = 6.

Constraints
  • 2 ≤ len(brackets) ≤ 50
  • brackets contains only ( and )
  • brackets is balanced
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTidy a Folder Route MediumFour-Operation Calculator MediumEvery Letter Once MediumChecking a Tree Record With Hash Marks MediumLongest Path to a File Medium
loading Python…
Input
brackets="()(())"
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 57 tests on the server, hidden ones included.