Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created May 12, 2016 00:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuadFlask/0f8770547f5082bad0bace86aa2828c0 to your computer and use it in GitHub Desktop.
Save QuadFlask/0f8770547f5082bad0bace86aa2828c0 to your computer and use it in GitHub Desktop.
[CodeWars] Valid Braces

괄호가 유효한지 체크

My Solution

function validBraces(braces){
  var stack = [];
  return braces.split('').map(b=> {
    if ('({['.indexOf(b)>=0) {
      stack.push(b);
      return true;
    } else {
      var e = stack.pop();
      return '({['.indexOf(e) == ')}]'.indexOf(b);
    }
  }).every(b=>b) && stack.length == 0;
}

Clever

function validBraces(braces){
  while(braces.indexOf("{}") != -1 || braces.indexOf("()") != -1 || braces.indexOf("[]") != -1){
    braces = braces.replace("{}", "").replace("()", "").replace("[]", "");
  }
  return braces.length == 0;
}
function validBraces(braces){
 while(/\(\)|\[\]|\{\}/g.test(braces)){braces = braces.replace(/\(\)|\[\]|\{\}/g,"")}
 return !braces.length;
}
@jhoffner
Copy link

You have publicly posted copyrighted content from Codewars.com. If any of these solutions are original works by you, you can leave them up but please remove all references to which kata they belong to or the fact that they are related to Codewars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment