๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/์ธํ”„๋Ÿฐ(inflearn)

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] section05 - 8 - ๋ชจ๋“  ์•„๋‚˜๊ทธ๋žจ ์ฐพ๊ธฐ

by YWTechIT 2021. 8. 30.
728x90

๐Ÿ“ section05 - 8 - ๋ชจ๋“  ์•„๋‚˜๊ทธ๋žจ ์ฐพ๊ธฐ

S ๋ฌธ์ž์—ด์—์„œ T๋ฌธ์ž์—ด๊ณผ ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” S์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ๊ฐ€ ์กฐ๊ธˆ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์š”๊ตฌ์‚ฌํ•ญ์„ ํ•˜๋‚˜์”ฉ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. ์ด์ „๊นŒ์ง€ ์•„๋‚˜๊ทธ๋žจ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋Š” hash์˜ ํŠน์ง•์„ ์‚ฌ์šฉํ–ˆ๊ณ , ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์€ slidingWindow์„ ์ด์šฉํ•˜์—ฌ ํ’€์—ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ธ๋ฑ์Šค ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด twoPointer ๋ฐฉ์‹๋งŒ ์ ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๋‚˜๋Š” while๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ, ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ํ’€์—ˆ๋Š”์ง€ ๊ถ๊ธˆํ•ด์„œ ์งˆ๋ฌธ์„ ๋‚จ๊ฒผ๋”๋‹ˆ ์„ ์ƒ๋‹˜๊ป˜์„œ ์นญ์ฐฌํ•ด์ฃผ์…จ๋‹ค.( โ˜บ๏ธ โ˜บ๏ธ ) ์ด๊ฒƒ๋ณด๋‹ค ๋” ์งง์€ ์ฝ”๋“œ๋กœ ์งœ๊ณ  ์‹ถ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

 

 

๋‚˜๋Š” ์ด๋ ‡๊ฒŒ ํ’€์—ˆ๋‹ค.

 

1. ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด rt++๊ฐ€ ๋˜๋ฉด์„œ sum์„ ๊ตฌํ•œ๋‹ค.

2. sum์˜ ๊ธธ์ด๊ฐ€ m๊ณผ ๋™์ผํ•˜๋ฉด ์—ฌ๊ธฐ์„œ hash๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

3. sHash๊ฐ’๊ณผ t๋ฅผ ๋น„๊ตํ•˜์—ฌ anagram์ด ๋งž๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  flag๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•œ๋‹ค. true | false

4. flag๊ฐ€ true๋ฉด cnt++

5. sum์˜ ๊ธธ์ด๊ฐ€ t.length๋ณด๋‹ค ์ปค์ง€๋ฉด ๊ทธ๋•Œ lt์˜ ๊ฐ’์„ ๋นผ์ค€๋‹ค.(๋นผ์ฃผ๋Š” ๊ณผ์ •์€ slice๋ฅผ ์ด์šฉํ•จ)

 

์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ๋ณด๋‹ˆ๊นŒ while๋ฌธ ๋งˆ๋‹ค for๋ฌธ์ด ๋‘ ๋ฒˆ ๋Œ๊ณ  slice๊ณผ์ •๊นŒ์ง€ ๊ฑฐ์น˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฝค ๋‚˜์˜ฌ ๊ฒƒ ๊ฐ™์•˜๋‹ค. ๊ทธ๋Ÿผ, while๋ฌธ ๋ฐ–์—์„œ ๋ฏธ๋ฆฌ hash๊ฐ’์„ ์„ ์–ธํ•˜๋ฉด ์–ด๋–จ๊นŒ? ์„ ์ƒ๋‹˜๊ป˜์„œ๋Š” ์ด๋ ‡๊ฒŒ ํ‘ธ์…จ๋‹ค.

 

1. for ์ „์— m-1๊นŒ์ง€ sum์„ ๊ตฌํ•œ๋‹ค.(์ดํ›„ while๋ฌธ์—์„œ sum ๊ฐ’์„ ๊ตฌํ•จ)

2. for ์ „์— t์˜ hash๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

3. rt๋ฅผ ๋Œ๋ฉด์„œ ํ˜„์žฌ hash๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

4. compareAnagram ํ•จ์ˆ˜๋ฅผ ๋Œ๋ฉด์„œ anagram์ด ๋งž๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.

5. ์ดํ›„ lt๊ฐ’์˜ value๋ฅผ 1์”ฉ ๋นผ์ฃผ๊ณ , ๋งŒ์•ฝ tH.get(value) === 0์ด๋ฉด, delete(key) ํ•ด์ค€๋‹ค. 0์ด ๋ ๋•Œ key๋Š” ๋” ์ด์ƒ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (ํ˜„์žฌ lt๋Š” t.length-3๋ฒˆ์งธ ์œ„์น˜ํ•œ๋‹ค.)

 

728x90

 

// ๋‚ด ์ฝ”๋“œ
let s = "bacaAacba";
let t = "abc";

console.log(solution(s, t));

function solution(s, t) {
  let n = s.length;
  let m = t.length;

  let sum = "";
  let lt = rt = cnt = 0;

  while (rt < n) {
    sum += s[rt];

    if (rt - lt + 1 >= m) {
      let sH = new Map();
      let temp = sum;
      let flag = true;

      for (let x of temp) {
        if (sH.has(x)) sH.set(x, sH.get(x) + 1);
        else sH.set(x, 1);
      }

      for (x of t) {
        if (!sH.has(x) || sH.get(x) === 0) flag = false;
        sH.set(x, sH.get(x) - 1);
      }

      if (flag) cnt++;
      sum = sum.slice(1);
      lt++;
    }
    rt++;
  }
  return cnt;
}

 

// ๊ฐ•์˜์ฝ”๋“œ
let s = "bacaAacba";
let t = "abc"

function compareAnagram(sH, tH) {
  if (sH.size !== tH.size) return false;

  for (let [key, value] of tH) {
    if (!sH.has(key)) return false;
    if (sH.get(key) !== value) return false;
  }
  return true;
}

console.log(solution(s, t));

function solution(s, t) {
  let n = s.length;
  let m = t.length;
  let cnt = lt = 0;

  let sH = new Map();
  let tH = new Map();

  for (let i = 0; i < m - 1; i++) {
    if (sH.has(s[i])) sH.set(s[i], sH.get(s[i]) + 1);
    else sH.set(s[i], 1);
  }

  for (let x of t) {
    if (tH.has(x)) tH.set(x, tH.get(x) + 1);
    else tH.set(x, 1);
  }

  for (let rt = m - 1; rt < n; rt++) {
    if (sH.has(s[rt])) sH.set(s[rt], sH.get(s[rt]) + 1);
    else sH.set(s[rt], 1);

    if (compareAnagram(sH, tH)) cnt++;

    sH.set(s[lt], sH.get(s[lt]) - 1);
    if (sH.get(s[lt]) === 0) sH.delete(s[lt]);
    lt++;
  }
  return cnt;
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€