Skip to content

Instantly share code, notes, and snippets.

@syul
Created February 6, 2019 11:15
Show Gist options
  • Save syul/b1f10c23c609bf31cea2e62cffd934b4 to your computer and use it in GitHub Desktop.
Save syul/b1f10c23c609bf31cea2e62cffd934b4 to your computer and use it in GitHub Desktop.
validation
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