Score of Parentheses
MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.Given a balanced string s of parentheses, compute its score with these rules:
()scores1.AB(two balanced strings side by side) scoresA + B.(A)(a balanced string wrapped in brackets) scores2 * A.
s = "()" → 1
s = "(())" → 2
s = "()()" → 2
s = "(()(()))" → 6 (2 * (1 + 2))Constraints
2 <= s.length <= 50scontains only(and).sis a balanced parentheses string.