KatInterview

886. Score of Parentheses

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Depth-based Score Calculation (O(1) Space)

The problem defines the score of a balanced parentheses string recursively. While this suggests a recursive or stack-based solution, a more efficient approach with O(1) space exists by observing the structure of the score.

Solution

Score of Parentheses in Python

class Solution:
    def scoreOfParentheses(self, s: str) -> int:
        score = 0
        depth = 0
        
        for i, char in enumerate(s):
            # If it's an opening parenthesis, we go one level deeper.
            if char == '(':
                depth += 1
            else:
                # If it's a closing parenthesis, we go one level up.
                depth -= 1
                
                # Check if this forms a basic "()" pair.
                # The first character of a balanced string is always '(',
                # so the first ')' must be at index i >= 1.
                if s[i-1] == '(':
                    # The score of a "()" pair is 2^d, where d is its depth.
                    # 1 << depth is equivalent to 2**depth.
                    score += 1 << depth
                    
        return score

Score of Parentheses in Java

class Solution {
    public int scoreOfParentheses(String s) {
        int score = 0;
        int depth = 0;

        for (int i = 0; i < s.length(); i++) {
            // If it's an opening parenthesis, we go one level deeper.
            if (s.charAt(i) == '(') {
                depth++;
            } else {
                // If it's a closing parenthesis, we go one level up.
                depth--;
                // Check if this forms a basic "()" pair.
                if (s.charAt(i - 1) == '(') {
                    // The score of a "()" pair is 2^d, where d is its depth.
                    // `1 << depth` is a fast way to compute 2^depth.
                    score += 1 << depth;
                }
            }
        }

        return score;
    }
}

Score of Parentheses in C++

class Solution {
public:
    int scoreOfParentheses(string s) {
        int score = 0;
        int depth = 0;

        for (int i = 0; i < s.length(); ++i) {
            // If it's an opening parenthesis, we go one level deeper.
            if (s[i] == '(') {
                depth++;
            } else {
                // If it's a closing parenthesis, we go one level up.
                depth--;
                // Check if this forms a basic "()" pair.
                if (s[i-1] == '(') {
                    // The score of a "()" pair is 2^d, where d is its depth.
                    // `1 << depth` is a fast way to compute 2^depth.
                    score += 1 << depth;
                }
            }
        }

        return score;
    }
};

Score of Parentheses in TypeScript

function scoreOfParentheses(s: string): number {
    let score = 0;
    let depth = 0;

    for (let i = 0; i < s.length; i++) {
        // If it's an opening parenthesis, we go one level deeper.
        if (s[i] === '(') {
            depth++;
        } else {
            // If it's a closing parenthesis, we go one level up.
            depth--;
            // Check if this forms a basic "()" pair.
            // The first character of a balanced string must be '(',
            // so i-1 is always a valid index for a ')' character.
            if (s[i - 1] === '(') {
                // The score of a "()" pair is 2^d, where d is its depth.
                // `1 << depth` is a fast way to compute 2^depth.
                score += 1 << depth;
            }
        }
    }

    return score;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.