작성
·
294
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));
모든 아나그램 찾기 코드리뷰 부탁드립니다!