Skip to content

Instantly share code, notes, and snippets.

@segaz2002
Created December 7, 2021 14:09
Show Gist options
  • Save segaz2002/ed977200ea91c1fceb06d70713caf2b5 to your computer and use it in GitHub Desktop.
Save segaz2002/ed977200ea91c1fceb06d70713caf2b5 to your computer and use it in GitHub Desktop.
Balanced Bracked solution v2
function isBalanced(s) {
// Write your code here
let agg = "";
let mapping = {"{": "}", "[": "]", "(": ")"};
let validBraces = ["()", "{}", "[]"];
for(let c of s){
//Check if opening
if(mapping[c]){
agg += c;
}else{
//check if match last opening
let opening = agg.substr(-1);
let brace = opening + c;
if(validBraces.includes(brace)){
agg = agg.substr(0, agg.length -1)
}else{
return "NO"
}
}
}
return (agg == "") ? "YES" : "NO";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment