[21/11/02 KATA NINJA] 압축

NinjaJuunzzi·2021년 11월 1일
0

코드카타

목록 보기
36/36
post-thumbnail

기존 풀이

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;
}
profile
Frontend Ninja

0개의 댓글