Created
March 19, 2019 16:03
-
-
Save marsgpl/9aea2cb26ab25350dc6b9700f2567913 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const check = function(seq) { | |
let opened = [] | |
for ( let char of seq ) { | |
if ( check.openers[char] ) { | |
opened.push(char) | |
} else { // closers | |
if ( opened.length==0 || opened[opened.length-1] !== check.closers[char] ) { | |
return false | |
} else { | |
opened.pop() | |
} | |
} | |
} | |
return true | |
} | |
check.openers = { | |
"{": "}", | |
"(": ")", | |
"[": "]", | |
} | |
check.closers = { | |
"}": "{", | |
")": "(", | |
"]": "[", | |
} | |
console.log("{()}[]", check("{()}[]")) | |
console.log("{[}]", check("{[}]")) | |
// const check = function(seq) { | |
// while ( seq.match(check.expr) ) { | |
// seq = seq.replace(check.expr, "") | |
// } | |
// return seq.length == 0 | |
// } | |
// check.expr = /\[\]|\(\)|\{\}/g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment