Created
December 7, 2021 14:09
-
-
Save segaz2002/ed977200ea91c1fceb06d70713caf2b5 to your computer and use it in GitHub Desktop.
Balanced Bracked solution v2
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
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