15% off all plans & roadmaps Use code
See plansLeetCode 20 · Solution & editorial
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
Valid Parentheses is a Stack Patterns problem, in the Valid Parentheses Matching subpattern. The pattern covers parentheses matching, monotonic stack, histogram, and expression evaluation
Master parentheses validation and balancing using stack. Handle multiple bracket types and minimum additions.
The reference solution runs in O(n) time and O(n) space.
Valid Parentheses has been reported in interviews at Amazon, Bloomberg, Facebook, Apple, Microsoft, Expedia, Spotify, Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Valid Parentheses is a Stack Patterns problem, in the Valid Parentheses Matching subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.