프로그래머스 LEVEL 2(영어 끝말잇기)
-
사용 언어 : javascript
-
해결 날짜 : 2022-09-11
- 해결 방법 :
- unique 단어를 저장하는 배열 생성 후 words를 돌며 word를 포함하거나, word의 첫 글자가 마지막 단어의 끝 글자와 다르면 [ 번호, 차례 ]를 반환
- 그 외의 경우 uniqueWords에 word push()
- 회고 :
- reduce 사용하기
-
코드
function solution(n, words) { const uniqueWords = []; for (const [index, word] of words.entries()) { if (uniqueWords.includes(word) || uniqueWords.length && uniqueWords[uniqueWords.length - 1].slice(-1) !== word.charAt(0)) { const num = (index + 1) % n === 0 ? n : (index + 1) % n; const order = parseInt(index / n) + 1; return [num, order]; } uniqueWords.push(word); } return [0, 0]; }
- 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges