easyStack / QueuePure DSA~15 min
JSON Bracket Validate
A lightweight JSON pre-validator checks that all the structural brackets in a payload are balanced before the heavier parser runs. Given a string of only the bracket characters () [] {}, decide whether every opener has a correctly ordered matching closer.
Problem
Given a string s containing only the characters '(', ')', '[', ']', '{', '}', return true if the brackets are balanced and correctly nested, otherwise false.
Input
A string s of length n (0 ≤ n ≤ 10^5) of bracket characters only.
Output
A boolean — true if balanced.
Constraints
- 0 ≤ n ≤ 100,000
- s contains only the six bracket characters
- An empty string is considered balanced
Examples
Example 1
Input
s = "{[()]}"Output
true
Each opener is closed by the matching type in the correct nested order.
Example 2
Input
s = "([)]"
Output
false
The ')' tries to close '[', a type mismatch, so the nesting is invalid.