Last active
May 5, 2020 22:53
-
-
Save rakibulalam/097372332ea9d7244876c6324f8d9a3d to your computer and use it in GitHub Desktop.
Balanced Brackets Hacker Rank
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
/*stack solutions*/ | |
function isBalanced(s) { | |
const brackets={'{':'}','(':')','[':']'}; | |
const arr=s.split(''); | |
const stack=[]; | |
/* if the length is odd*/ | |
if(arr.length%2!==0) return 'NO'; | |
for(let i=0; i<arr.length; i++) | |
{ | |
const op=brackets[arr[i]]; | |
if(op){ | |
stack.push(op) | |
}else if(stack[stack.length-1]===arr[i]){ | |
stack.pop() | |
}else{ | |
stack.push(op); | |
break; | |
} | |
} | |
return stack.length===0?'YES':'NO' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment