Skip to main content
LeetCode 856, Medium. Topics: String, Stack, Bracket Sequences. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 23 parametrized pytest cases, and a playground notebook:

Problem

Given a balanced parentheses string s, return the score of the string. The score of a balanced parentheses string is based on the following rule:
  • "()" has score 1.
  • AB has score A + B, where A and B are balanced parentheses strings.
  • (A) has score 2 * A, where A is a balanced parentheses string.

Examples

Constraints

  • 2 <= s.length <= 50
  • s consists of only '(' and ')'.
  • s is a balanced parentheses string.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026