Algorithm/codewars

Valid Braces 답안

Bonita SY 2021. 3. 24. 18:48
728x90
반응형

문제)

www.codewars.com/kata/5277c8a221e209d3f6000b56/train/javascript

 

Codewars: Achieve mastery through challenge

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

내가 푼 답)

function validBraces(braces){
  var braceList = braces.split('');
  var openBrace = [];
  for (var brace of braceList) {
    if (['(', '{', '['].includes(brace)) {
      openBrace.push(brace);
    } else {
      if (openBrace.length !== 0) {
        if (brace === ')') {
          if (openBrace[openBrace.length - 1] === '(') {
            openBrace.pop();
          } else {
            return false;
          }
        } else if (brace === '}') {
          if (openBrace[openBrace.length - 1] === '{') {
            openBrace.pop();
          } else {
            return false;
          }
        } else {
          if (openBrace[openBrace.length - 1] === '[') {
            openBrace.pop();
          } else {
            return false;
          }
        }
      } else {
        return false;
      }
    }
  }
  
  if (openBrace.length !== 0) {
    return false;
  } else {
    return true;
  }
}

 

테스트 결과)

 

마음에 드는 다른 사람이 푼 답)

ㄷㄷ 신인가

728x90
반응형

'Algorithm > codewars' 카테고리의 다른 글

Are they the "same"? 답안  (0) 2021.03.24
Counting Duplicates 답안  (0) 2021.03.24
Calculating with Functions 답안  (0) 2021.03.24
Is this a triangle? 답안  (0) 2021.03.23
Exes and Ohs 답안  (0) 2021.03.23