기존 풀이
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",
];
function getWC(string, current, next, dict) {
if (next >= string.length)
return [dict.indexOf(string.slice(current, next)) + 1, next - current - 1];
const w = string.slice(current, next);
const c = string.slice(next, next + 1);
const index = dict.indexOf(w + c);
if (index === -1) {
dict.push(w + c);
return [dict.indexOf(w) + 1, next - current - 1];
} else {
return getWC(string, current, next + 1, dict);
}
}
function solution(msg) {
var answer = [];
for (let check = 0; check < msg.length; check++) {
const [print, depth] = getWC(msg, check, check + 1, dict);
answer.push(print);
check += depth;
}
return answer;
}