The LIFO Requirement
When you open a `[`, you cannot close it until any inner brackets like `{` are completely resolved. The last bracket opened MUST be the first bracket closed. This maps perfectly to a Stack.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Given a string containing just the characters '(', ')', ', ', '[' and ']', determine if the input string is structurally valid.
When you open a `[`, you cannot close it until any inner brackets like `{` are completely resolved. The last bracket opened MUST be the first bracket closed. This maps perfectly to a Stack.
Iterate through the string. If it's an opening bracket, push it. If it's a closing bracket, peek the stack. If it matches the corresponding opening bracket, pop it. Otherwise, the string is invalid.
1. Closing bracket with an empty stack (e.g., `]`).
2. Reaching the end of the string but the stack isn't empty (e.g., `[`). In both cases, return false.