A diagnostic tool encodes a nested amplifier chain as a string of only ( and ) characters, guaranteed to be balanced (every prefix has at least as many ( as ), and the total counts are equal). The chain's signal score is defined recursively:
() (a single directly-adjacent pair) has score 1.AB is the concatenation of two balanced pieces A followed by B (each balanced on its own), its score is score(A) + score(B).(A) (a single pair wrapping a balanced piece A), its score is 2 * score(A).Compute the signal score of the whole given string.
Line 1: the balanced parentheses string.
A single integer: the signal score of the string.
Example 1
Input
()
Expected
1
Explanation
A single directly-adjacent pair has score 1.
Example 2
Input
(())
Expected
2
Explanation
The inner '()' has score 1, and wrapping it in one more pair doubles the score: 2*1=2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →