Skip to content

Instantly share code, notes, and snippets.

@chukitow
Created October 28, 2015 22:53
Show Gist options
  • Save chukitow/0b7df645891954eca8ed to your computer and use it in GitHub Desktop.
Save chukitow/0b7df645891954eca8ed to your computer and use it in GitHub Desktop.
Given a String formed only by parenthesis check if they are well balanced (Every parenthesis that opens closes).
var stringGiven = "(()()(()))",
auxContainer = [],
finalContainer = [];
for(var i=0; i < stringGiven.length; i++){
if(stringGiven[i] === "("){
auxContainer.push(stringGiven[i]);
}
else{
if(auxContainer.length){
finalContainer.push(auxContainer.pop());
finalContainer.push(stringGiven[i]);
}
}
}
console.log(finalContainer.join('').length === stringGiven.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment