Algorithm/codewars

Permutations 답안

Bonita SY 2021. 3. 29. 19:40
728x90
반응형

문제)

www.codewars.com/kata/5254ca2719453dcc0b00027d/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 permutations(string) {
  const result = [];
  
  numberOfCases(result, string);
  
  return result;
}

function numberOfCases(result, string) {
  let charList = string.split('');
  
  for (let i=0; i<charList.length; i++) {
    for (let j=0; j<charList.length; j++) {
      let tmp_strs = [...charList];
      tmp_strs.splice(i, 1, charList[j]);
      tmp_strs.splice(j, 1, charList[i]);
      
      let tmp_str = tmp_strs.join('');
      if (!result.includes(tmp_str)) {
        result.push(tmp_str);
        numberOfCases(result, tmp_str);
      }
    }
  }
}

 

테스트 결과)

 

마음에 드는 남이 푼 답)

728x90
반응형

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

Sum by Factors 답안  (0) 2021.03.30
Number of trailing zeros of N! 답안  (0) 2021.03.29
Recover a secret string from random triplets 답안  (0) 2021.03.29
Greed is Good 답안  (0) 2021.03.24
Moving Zeros To The End 답안  (0) 2021.03.24