Created
February 6, 2019 11:15
-
-
Save syul/b1f10c23c609bf31cea2e62cffd934b4 to your computer and use it in GitHub Desktop.
validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValid(inStr = '') { | |
var dictionary = { | |
'{': '}', | |
'(': ')', | |
'[': ']' | |
} | |
var stack = []; | |
inStr = inStr.split(''); | |
for (var i = 0; i < inStr.length; i++) { | |
if(!dictionary.hasOwnProperty(inStr[i]) && dictionary[stack[stack.length -1]] == inStr[i]) { | |
stack.pop(); | |
} else if(dictionary.hasOwnProperty(inStr[i])) { | |
stack.push(inStr[i]); | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} | |
console.log(isValid('[{}{}{}()]')); // true | |
console.log(isValid('[{}{}{]}()]')); // false | |
console.log(isValid('[{}{]}{}()]')); // false | |
console.log(isValid('[{({})}{[()]}[({})]()]')); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment