Back to October Back to Year 2026
MEDIUM 05 Oct 2026 View on LeetCode

856. Score of Parentheses

</> Solution

/**
 * Problem: LeetCode 856 - Score of Parentheses
 * Difficulty: Medium
 * Language: Java
 * Author: CodeByTushu
 */
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) where N = s.length

SPACE COMPLEXITY

O(1)

TOPICS

Bit Manipulation Math Stack String