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