// 풀이 #1
function solution(msg){
msg=msg.split('');
let messageCopy=msg.slice();
let answer=[];
let alphabets={};
let indexIs=27;
for (let i=1;i<='Z'.charCodeAt()-64;i++){
alphabets[String.fromCharCode(i+64)]=i;
}
while(messageCopy.length){
// 제일 긴 w 찾기
let w=messageCopy.shift();
let c='';
for (let i=0;i<messageCopy.length;i++){
let newW=w+messageCopy[i];
// 하나 더 해도 존재하는 경우
if (alphabets[newW]!==undefined){
w=newW;
c=messageCopy[i+1]
}else{
c=messageCopy[i]
// 존재하지 않는 경우
break;
}
}
if (c!==undefined){
answer.push(alphabets[w])
alphabets[w+c]=indexIs;
indexIs+=1;
messageCopy.splice(0, w.length-1)
}else{
answer.push(alphabets[w])
messageCopy.splice(0, w.length-1)
}
}
return answer
}
// 풀이 #2
function solution(msg) {
msg=msg.split('')
let messageCopy=msg.slice();
var answer = [];
let alphabets={};
for (let i=1;i<='Z'.charCodeAt(0)-64;i++){
alphabets[String.fromCharCode(i+64)]=i
}
let lastCount=27
const s = messageCopy.reduce((w, c) => {
let nextWord = w + c;
if (alphabets[nextWord] === undefined) {
alphabets[nextWord] = lastCount++;
} else {
return w + c;
}
if (alphabets[w] !== undefined) answer.push(alphabets[w]);
return c;
});
answer.push(alphabets[s]);
return answer;
}