Last active
July 7, 2016 12:17
-
-
Save PunkSage/5edcdf2ceda6300e07c02c1164d606eb to your computer and use it in GitHub Desktop.
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
/* Verify if a given string has a proper parenthesis setup */ | |
function verify(str) { | |
var array = str.split(''); | |
var stack = []; | |
var left = ['{','[','(']; | |
var right = ['}',']',')']; | |
var opposite = { | |
')':'(', | |
'}':'{', | |
']':'[', | |
} | |
while (array.length > 0) { | |
var x = array.shift(); | |
if (right.indexOf(x) > -1) { | |
if (stack.pop() !== opposite[x]) return false; | |
} else { | |
stack.push(x); | |
} | |
} | |
return (stack.length === 0); | |
} | |
console.log('empty',verify('')); | |
console.log('{[]{}}',verify('{[]{}}')); | |
console.log('(}{)',verify('(}{)')); | |
console.log('}{',verify('}{')); | |
console.log('{}',verify('{}')); | |
console.log('})]',verify('})]')); | |
console.log('{(}',verify('{(}')); | |
console.log('({})',verify('({})')); | |
console.log('([)]',verify('([)]')); | |
console.log('{{}}',verify('{{}}')); | |
console.log('()',verify('()')); | |
console.log('(()',verify('(()')); | |
console.log(')(',verify(')(')); | |
console.log('(())()()',verify('(())()()')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment