ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

S๋ฌธ์ž์—ด์—์„œ T๋ฌธ์ž์—ด๊ณผ ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” S์˜ ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์•„๋‚˜๊ทธ๋žจ ํŒ๋ณ„์‹œ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ณ„๋ฉ๋‹ˆ๋‹ค. ๋ถ€๋ถ„๋ฌธ์ž์—ด์€ ์—ฐ์†๋œ ๋ฌธ์ž์—ด์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ œํ’€์ด


  • Map1 ๊ฐ์ฒด์— ๋งจ ์ฒ˜์Œ ๋ฌธ์ž์—์„œ๋ถ€ํ„ฐ ์—ฐ์†๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ์ตœ๋Œ€ ๋ฌธ์ž์—ด ๊ธธ์ด๊นŒ์ง€์˜ ๋ฌธ์ž์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ(Key, Value)๋ฅผ ์ ์žฌํ•œ๋‹ค.
  • ๋น„๊ต์˜ ๊ธฐ์ค€์ด ๋  Map2 ๊ฐ์ฒด์— 0~(T๋ฌธ์ž์—ด ๊ธธ์ด - 1) ๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์ ์žฌํ•œ๋‹ค.
  • ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” ์ดˆ๊ธฐ์— Map2 ๊ฐ์ฒด์— a์™€ b๊ฐ€ ์ ์žฌ๋œ๋‹ค.
  • ๋‹ค์Œ ์ธ๋ฑ์Šค ์š”์†Œ์—์„œ๋ถ€ํ„ฐ ๋ฐ˜๋ณต๋ฌธ์„ ์‹œ์ž‘ํ•˜์—ฌ Map1 ๊ฐ์ฒด์— ํฌ์ธํ„ฐ2๊ฐ€ ํ˜„์žฌ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์ธ๋ฑ์Šค(์‹œ์ž‘ ์ธ๋ฑ์Šค ์š”์†Œ)์˜ ๋ฌธ์ž๋ฅผ ์ ์žฌํ•œ๋‹ค.
  • ๋‘ Map ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•œ๋‹ค.
  • ๋น„๊ต๊ฐ€ ๋๋‚œ ๋’ค์— ํฌ์ธํ„ฐ1์ด ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์ธ๋ฑ์Šค ์š”์†Œ๋ฅผ Map1 ๊ฐ์ฒด์—์„œ ์‚ญ์ œํ•œ๋‹ค.
  • ์‚ญ์ œ ์‹œ, value ๊ฐ’์ด 0์ผ ๊ฒฝ์šฐ์—๋Š” ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— delete ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด Key๊ฐ’์„ ์™„์ „ํžˆ ์‚ญ์ œํ•ด์ฃผ๊ณ  ์•„๋‹ ๊ฒฝ์šฐ์—๋Š” 1๋งŒํผ ๊ฐ์‚ฐํ•ด์ค€๋‹ค.

์ฝ”๋“œ

function compareMaps(map1, map2) {
  if(map1.size !== map2.size) return false;
  for(let [key, val] of map1) {
    if(!map2.has(key) && map2.get(key) === val) return false;
  }
  return true;
}

function solution(s1, s2) {
  let answer = 0;
  let sH1 = new Map();
  let sH2 = new Map();
  let p1 = 0;
  let p2 = 0;

  for (let i = 0; i < s2.length; i++) {
    if (!sH1.has(s1[i]) || sH2.has(s2[i])) {
      sH1.set(s1[i], 1);
      sH2.set(s2[i], 1);
    } else {
      sH1.set(s1[i], sH1.get(s1[i]) + 1);
      sH2.set(s2[i], sH2.get(s2[i]) + 1);
    }
  }

  for(let i = 0; i < s1.length; i++) {
    if(!sH1.has(s1[++p2])) {
      sH1.set(s1[p2], 1);
    } else {
      sH1.set(s1[p2], sH1.get(s1[p2]) + 1);
    }
    if((p2 - p1) === 2) {
    	//๋‘ ํฌ์ธํ„ฐ๊ฐ€ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ์ธ๋ฑ์Šค ๊ฐ„ ์ฐจ์ด๊ฐ€ 2์ผ ๊ฒฝ์šฐ => ์—ฐ์†๋ถ€๋ถ„์ˆ˜์—ด์˜ ์›์†Œ ๊ฐฏ์ˆ˜ ์กฐ๊ฑด์— ํ•ฉ๋‹นํ•  ๊ฒฝ์šฐ
      let result = compareMaps(sH1, sH2);
      if(result ===  true) answer++;
      sH1.delete(s1[p1++]);
    }
  }
  return answer;
}

let str1 = "bacaAacba";
let str2 = "abc";
console.log(solution(str1, str2));

function compareMaps(map1, map2){
                if(map1.size!==map2.size) return false;
                for(let [key, val] of map1){
                    if(!map2.has(key) || map2.get(key)!==val) return false;
                }
                return true;
            }
            function solution(s, t){
                let answer=0;
                let tH = new Map();
                let sH = new Map();
                for(let x of t){
                    if(tH.has(x)) tH.set(x, tH.get(x)+1);
                    else tH.set(x, 1);
                }
                let len=t.length-1;
                for(let i=0; i<len; i++){
                    if(sH.has(s[i])) sH.set(s[i], sH.get(s[i])+1);
                    else sH.set(s[i], 1);
                }
                let lt=0;
                for(let rt=len; rt<s.length; rt++){
                    if(sH.has(s[rt])) sH.set(s[rt], sH.get(s[rt])+1);
                    else sH.set(s[rt], 1);
                    if(compareMaps(sH, tH)) answer++;
                    sH.set(s[lt], sH.get(s[lt])-1);
                    if(sH.get(s[lt])===0) sH.delete(s[lt]);
                    lt++;
                }
                return answer;
            }
            
            let a="bacaAacba";
            let b="abc";
            console.log(solution(a, b));

       

 

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ