Skip to content

Instantly share code, notes, and snippets.

@rakibulalam
Last active May 5, 2020 22:53
Show Gist options
  • Save rakibulalam/097372332ea9d7244876c6324f8d9a3d to your computer and use it in GitHub Desktop.
Save rakibulalam/097372332ea9d7244876c6324f8d9a3d to your computer and use it in GitHub Desktop.
Balanced Brackets Hacker Rank
/*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