• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

코드 리뷰 부탁드립니다!

23.01.18 00:59 작성 조회수 230

0

function compareMaps(mapA, mapB) {
  if (mapA.size !== mapB.size) {
    return false;
  }
  for (let [key, value] of mapA) {
    if (!mapB.has(key) || mapB.get(key) !== value) {
      return false;
    }
  }
  return true;
}

function solution(map1, map2) {
  let mapA = new Map();
  let mapB = new Map();
  let lt = 0;
  let n = map2.length;
  let answer = 0;
  for (let rt = 0; rt < map1.length; rt++) {
    if (rt < n) mapB.set(map2[rt], mapB.get(map2[rt]) + 1 || 1);
    mapA.set(map1[rt], mapA.get(map1[rt]) + 1 || 1);

    if (rt >= n) {
      mapA.get(map1[lt]) == 1
        ? mapA.delete(map1[lt])
        : mapA.set(map1[lt], mapA.get(map1[lt]) - 1);
      lt++;
    }
    if (rt >= n - 1) {
      if (compareMaps(mapA, mapB)) answer++;
    }
  }

  return answer;
}

let a = "bacaAacba";
let b = "abc";
console.log(solution(a, b));

모든 아나그램 찾기 코드리뷰 부탁드립니다!

답변 1

답변을 작성해보세요.

0

안녕하세요^^

네. 코드가 더 간결해 보이고 좋네요. 잘 하신 코드입니다.