Valid Parentheses
Determine whether a parentheses string is well-formed
Determine if the parentheses string is well-formed.
Examples
Example 1
Input: s = "()" Output: true
Example 2
Input: s = "(]" Output: false
Constraints
- 1 ≤ |s| ≤ 10⁴
Tips
- Push opening brackets onto a stack.
- Pop only if the current closing bracket matches the stack top.
Hints
Show hint 1Hint 1
Map each closing bracket to its matching opening bracket.
Show hint 2Hint 2
Early exit when the stack is empty but a closing bracket appears.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you validate HTML/XML-like tags where tokens can be multi-character?
- Can you extend this to handle wildcard characters such as '*'?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem