Skip to content

Instantly share code, notes, and snippets.

@vidul-nikolaev-petrov
Last active October 14, 2016 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vidul-nikolaev-petrov/4f8d69e145e1df3d47732b1c63229e1d to your computer and use it in GitHub Desktop.
Save vidul-nikolaev-petrov/4f8d69e145e1df3d47732b1c63229e1d to your computer and use it in GitHub Desktop.
Balanced Brackets
function checkBalance(s) {
if (s.length % 2 !== 0) return false;
if (s.length === 0) return true;
var pairs = ['[]', '()', '{}'],
r0 = s.split(pairs[0]),
r1 = s.split(pairs[1]),
r2 = s.split(pairs[2]);
if (r0.length > 1) return checkBalance(r0.join(''));
if (r1.length > 1) return checkBalance(r1.join(''));
if (r2.length > 1) return checkBalance(r2.join(''));
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment