You are given a string s consisting of the following characters: '(', ')', '{', '}', '[' and ']'.
The input string s is valid if and only if:
Return true if s is a valid string, and false otherwise.
Example 1:
Input: s = "[]"
Output: trueExample 2:
Input: s = "([{}])"
Output: trueExample 3:
Input: s = "[(])"
Output: falseExplanation: The brackets are not closed in the correct order.
Constraints:
1 <= s.length <= 1000class Solution {
OPEN_PARENS;
PAREN_MAP = {
')': '(',
']': '[',
'}': '{',
};
constructor() {
this.OPEN_PARENS = new Set(Object.values(this.PAREN_MAP))
}
/**
* @param {string} s
* @return {boolean}
*/
isValid(s) {
const parens = [];
for (const char of s) {
if (parens.length === 0 && !this.OPEN_PARENS.has(char)) {
return false;
}
const lastParen = parens.at(-1);
if (parens.length > 0 && lastParen === this.PAREN_MAP[char]) {
parens.pop();
continue;
}
parens.push(char);
}
return parens.length === 0;
}
}