Dsa
Math
Medium
Balanced Brackets Score
You have a string of brackets that includes '(', ')', '{', '}', '[' and ']'. Each bracket has a score based on its type:
- '(': 1
- ')': 1
- '{': 2
- '}': 2
- '[': 3
- ']': 3
A sequence of brackets is deemed balanced if every opening bracket has a corresponding closing bracket in the correct order. Calculate the total score of a balanced string.
Input:
A single line string consisting of brackets (1 <= length <= 10^5).
Output:
An integer representing the score of the balanced brackets or -1 if the string is not balanced.
Example:
Input:
{[()]}
Output:
8
Explanation: The score is 2 (from '{') + 3 (from '[') + 1 (from '(') + 1 (from ')') + 3 (from ']') + 2 (from '}') = 8.
Key concepts
mathstackbracket
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now