validate string of parentheses
2022年5月1日小于 1 分钟
validate string of parentheses
Question
Given a string containing only following characters:
- parentheses :
(
or)
- brackets:
[
or]
- braces:
{
or}
write a function to determine if they are valid.
By 'valid', it means all should be rightly paired, and with the valid order.
validate('{}[]()')
// true
validate('{[()]}')
// true
validate('{[}]')
// false, they are not in the right order
validate('{}}')
// false, last `}` is not paired with `{`
Follow-up
What is time & space complexity of your approach ? Can you do it better?
Code
/**
* @param {string} str
* @return {boolean}
*/
function validate(str) {
// your code here
}