프로그래머스 LEVEL 2(압축)
-
사용 언어 : javascript
-
해결 날짜 : 2022-09-25
- 해결 방법 :
- dict에 A ~ Z까지 초기화
- msg를 돌며 i 다음 인덱스들을 포함한 값이 dict에 있다면 temp에 추가
- dict에 없다면 answer에 그 전 temp값 까지의 값 push() 및 dict에 temp + msg[j] push()
- 회고 :
- x
-
코드
function solution(msg) { const answer = []; const dict = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; let temp = ''; for (let i = 0; i < msg.length; i++) { temp += msg[i]; if (i === msg.length - 1 && dict.includes(temp)) answer.push(dict.indexOf(temp) + 1); for (let j = i + 1; j < msg.length; j++) { if (dict.includes(temp + msg[j])) { temp += msg[j]; i++; if (j === msg.length - 1) answer.push(dict.indexOf(temp) + 1); } else { answer.push(dict.indexOf(temp) + 1); dict.push(temp + msg[j]); temp = ''; break; } } } return answer; }
- 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges