Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1 ≤ s.length ≤ 10⁴s consists of the characters '()[]{}' only.Push opening brackets, pop when a matching close arrives.
Read the string left to right, the way you'd check brackets in code by eye. An opening bracket is a promise: something later has to close it. A closing bracket keeps a promise, but not just any of them. It has to close the newest opening bracket that's still open. In {[()]}, the ) must close the (, because the [ and { were opened before it and are still waiting outside it.
Time O(n)Space O(n)
The full walkthrough and code in five languages, free to read
true