Question:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example:
Input: "()"Output: trueInput: "()[]{}"Output: trueInput: "(]"Output: falseInput: "([)]"Output: falseInput: "{[]}"Output: true
Solution:
/*** @param {string} s* @return {boolean}*/var isValid = function(s) {const error = [];const map = {')': '(','}': '{',']': '['};const arr = s.split('');for (let i = 0; i < arr.length; i++) {if (error && map[arr[i]] && map[arr[i]] == error[error.length-1]) {error.pop();}else{error.push(arr[i]);}}return error.length === 0};
Runtime: 52 ms, faster than 99.86% of JavaScript online submissions for Valid Parentheses.
