Red-Green-Code

Deliberate practice techniques for software developers

  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter

LeetCode 856: Score of Parentheses

By Duncan Smith Mar 3 0

LeetCode 2021

Problem

LeetCode 856: Score of Parentheses (Medium)

Problem Statement: You are given a string containing only the characters ( and ). The parentheses are guaranteed to be balanced (as in syntactically valid source code). Calculate a score using the following rules:

  • () has a score of 1.
  • AB has a score of A + B, where A and B are balanced strings of parentheses.
  • (A) has a score of 2 * A, where A is a balanced string of parentheses.

Solution

In the problem LeetCode 20: Valid Parentheses, we have to check if a string of parentheses is balanced. In this problem, the input is guaranteed to be balanced, so we don’t need to verify that condition. But some of the techniques used to verify balance are also useful in this problem.

Given the recursive definition in the problem statement, it seems like the problem might lend itself to a recursive solution. The free official solution uses a recursive function in Approach 1, which requires $O(n^2)$ time and $O(n)$ space. Balanced parentheses problems can also generally be solved using a stack, which is how it’s done in Approach 2. That solution reduces the time requirement to $O(n)$. I’m going to explain Approach 3, which uses no stack or other data structures, and requires only $O(1)$ space.

Solution idea

Define a core as the substring (). A core has a score of 1. When a core is surrounded by parentheses, each set of surrounding parentheses multiplies what’s inside by 2. So () is 1, (()) is 2, ((())) is 4, and so on. If two or more substrings are arranged side by side, their scores are added together. So ()() is $1+1=2$, ()()() is $1+1+1=3$, and (())(())(()) is $2+2+2=6$. Because of these rules, the total score can always be expressed as the sum of powers of two.

Next, define the balance of a substring as the number of opening parentheses in the substring minus the number of closing parentheses. The balance of the full input string is always 0. But if we iterate through the input string character by character, we can increment the balance at each ( and decrement it at each ), and the balance at each step will be a non-negative integer that we can use to calculate a running score.

Here’s the key idea: Whenever we see () (a core), we increase the current score by $2^b$, where $b$ is the current balance. This works because $b$ is the number of parentheses the core is contained in, and each of those parentheses multiplies the core value by $2$. So for (()), we’ll have $b=1$ when we encounter the core, so we’ll correctly evaluate the score as $2^1=2$. For (())(())(()), we’ll have $b=1$ at each core, so we’ll add $2$ to the score three times.

A quick note on code syntax: 1 << b means start with 1 and shift it left b times. So it’s the same as $2^b$ or Math.Pow(2, b).

Pseudocode

Variables (both ints, initialized to zero):

  • score: the problem result
  • balance: the current balance of parentheses

    for each char c in input string
        if c is a left paren, increment balance
        else
            decrement balance
            if the previous char was a left paren
                increment score by 2^balance
    return score
    

As described in the solution idea, we iterate through the input one character at a time, incrementing the current balance when we see ( and decrementing it when we see ). Also, when we see (), we increase the result by 2 raised to the power of the current balance.

Code

Score of Parentheses on GitHub

LeetCode Model Solutions

I’m posting some LeetCode model solutions this year. For more about this, see A Project for 2021.

Categories: LeetCode

Prev
Next

Stay in the Know

I'm trying out the latest learning techniques on software development concepts, and writing about what works best. Sound interesting? Subscribe to my free newsletter to keep up to date. Learn More
Unsubscribing is easy, and I'll keep your email address private.

Getting Started

Are you new here? Check out my review posts for a tour of the archives:

  • 2023 in Review: 50 LeetCode Tips
  • 2022 in Review: Content Bots
  • 2021 in Review: Thoughts on Solving Programming Puzzles
  • Lessons from the 2020 LeetCode Monthly Challenges
  • 2019 in Review
  • Competitive Programming Frequently Asked Questions: 2018 In Review
  • What I Learned Working On Time Tortoise in 2017
  • 2016 in Review
  • 2015 in Review
  • 2015 Summer Review

Archives

Recent Posts

  • Do Coding Bots Mean the End of Coding Interviews? December 31, 2024
  • Another Project for 2024 May 8, 2024
  • Dynamic Programming Wrap-Up May 1, 2024
  • LeetCode 91: Decode Ways April 24, 2024
  • LeetCode 70: Climbing Stairs April 17, 2024
  • LeetCode 221: Maximal Square April 10, 2024
  • Using Dynamic Programming for Maximum Product Subarray April 3, 2024
  • LeetCode 62: Unique Paths March 27, 2024
  • LeetCode 416: Partition Equal Subset Sum March 20, 2024
  • LeetCode 1143: Longest Common Subsequence March 13, 2024
Red-Green-Code
  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter
Copyright © 2025 Duncan Smith