Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
Solution
var isValid = function(s) {
let arr = [];
const bracketObj = {
"{":"}",
"(":")",
"[":"]"
}
for(let i=0;i<s.length;i++){
if(s[i] =="{"|| s[i] == "(" || s[i] == "["){
arr.push(s[i]);
}else if(bracketObj[arr.pop()] !== s[i]){
return false;
}
}
return arr.length === 0 ;
};
Test Cases Example
Input: s = "()"
Output: true
Input: s = "()[]{}"
Output: true
Input: s = "(]"
Output: false
Input: s = "([)]"
Output: false
Input: s = "(("
Output: false`
**
CodeSandBox Link **
https://codesandbox.io/s/valid-parentheses-1gs732?file=/src/index.js
Top comments (0)