Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-177

Score of Parentheses

Problem

Compute the score of a balanced parentheses string where () scores 1, concatenation adds scores, and wrapping a group in parentheses doubles its score.

Use a stack of running scores: push a new zero on each opening bracket, and on a closing bracket add either 1 or twice the popped value to the new top. Return 0 for the empty string.

Hint

A popped score of zero means the pair was an empty (), which is worth 1.

Test cases

case 1
in: ["()"]
expected: 1
case 2
in: ["(())"]
expected: 2
case 3
in: ["()()"]
expected: 2
case 4
in: ["(()(()))"]
expected: 6
case 5
in: [""]
expected: 0
Starter codePython
def score_of_parentheses(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor