Nested Bracket Score Calculation
Solve this Problems containing only '(' and ')', compute its score using these rules: "()" scores 1; two concatenated pieces AB score the sum of their individual scores; and a piece wrapped as (A) scores twice ADoubling on NestingWrapping any scored piece A in one more layer of parentheses always doubles its score — that's the entire rule that turns "how deeply nested" into "what power of 2 to multiply by." — nesting doubles whatever score is inside.
Since nesting always doubles and concatenation always adds, every innermost "()" pair's ultimate contribution to the total is exactly 2^d, where d is how many enclosing pairs wrap around it. A single pass with a stack computes this without ever explicitly calculating a power of 2: push a fresh 0 for every new level opened, and whenever a level closes, fold its score into the level directly beneath it — doubling it first if it wasn't just a bare pair. By the time the string is fully consumed, the bottom of the stack holds the total.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ s.length ≤ 20 - ◆
s is a balanced bracket string consisting only of '(' and ')'
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
Approach & Solutions
Brute Force — Depth-Count Every Matched Pair
BruteEvery innermost "()" pair contributes 2^d to the total score, where d is how many levels of nesting enclose it — and every score in this problem ultimately traces back to summing that contribution over each such pair (concatenation just adds scores, nesting just doubles them). So: scan for every adjacent "()" pair in the string, and for each one, count its nesting depth by re-scanning everything before it and tracking the running balance of unmatched '(' versus ')'. Sum 2^depth over every pair found. Correct, but re-scanning from the start for every pair costs O(n) each, and there can be up to n/2 pairs — O(n²) total.
O(n²)O(1)1class Solution {
2 public int scoreOfBrackets(String s) {
3 int total = 0;
4 int n = s.length();
5 for (int i = 0; i < n - 1; i++) {
6 if (s.charAt(i) == '(' && s.charAt(i + 1) == ')') {
7 int depth = 0;
8 for (int k = 0; k < i; k++) {
9 if (s.charAt(k) == '(') depth++;
10 else depth--;
11 }
12 total += 1 << depth;
13 }
14 }
15 return total;
16 }
17}Optimal — Single Pass With a Stack of Partial Scores
OptimalTrack the running score at each nesting level on a stack, seeded with a 0 at the bottom for the outermost level. On '(', push a fresh 0 — a new level starts with no score yet. On ')', pop the just-finished level's score: if it was 0 (nothing accumulated inside), this was an innermost "()" pair, worth 1; otherwise, double whatever accumulated inside it (nesting doubles). Either way, add that value into the level directly below, which is now back on top of the stack. One pass, one push or pop per character — no re-scanning.
O(n)O(n)1class Solution {
2 public int scoreOfBrackets(String s) {
3 Deque<Integer> stack = new ArrayDeque<>();
4 stack.push(0);
5 for (char c : s.toCharArray()) {
6 if (c == '(') {
7 stack.push(0);
8 } else {
9 int inner = stack.pop();
10 int score = (inner == 0) ? 1 : inner * 2;
11 stack.push(stack.pop() + score);
12 }
13 }
14 return stack.pop();
15 }
16}