Practice arena
C++17·2 s·128 MB
LinearFoundation
Balanced brackets
Validate (), [] and {}. Print -1 when valid; otherwise print the zero-based index of the first bad closing bracket, or the earliest opening bracket left unmatched at the end.
Input
- — The first line contains T.
- — Each case is one non-empty bracket string.
Output
Print the first bad index, or -1 when the string is balanced.
Technique
stack of token and index
Target
O(n) time · O(n) worst-case space
Constraints & sample
- 1 ≤ |s| ≤ 200,000
- s contains only ()[]{}
Sample input
4
([]{})
([)]
(((
{[()]}Expected output
-1 2 0 -1
Before you submit
- Test the smallest valid input and one awkward edge case.
- Keep the solution within O(n) time · O(n) worst-case space.
- Match spacing and line breaks exactly before checking 10 hidden cases.
Draft saved locally · 356 / 30,000
Ln 1, Col 1Spaces: 4UTF-8LFC++17
Visible testcases
Editable and stored only in this browser. Hidden judge cases never change.
Checking your session…
Run checks Sample · Submit checks 10 hidden cases
Run the sample first, then send your solution through 10 hidden cases.
Code runs in an isolated Judge0 environment. Drafts and custom visible cases stay on this device; submission metadata and progress sync to MongoDB.