Back to Month
MEDIUM 05 Oct 2026 View on LeetCode

856. Score of Parentheses

</> Solution

class Solution {
    public int scoreOfParentheses(String s) {
        int score = 0;
        int depth = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                depth++;
            } else {
                depth--;
                // Direct "()" pair found
                if (s.charAt(i - 1) == '(') {
                    score += 1 << depth;
                }
            }
        }
        return score;
    }
}

TIME COMPLEXITY

O(n)

SPACE COMPLEXITY

O(1)

TOPICS

Data Structure Parentheses String