์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ธํ”„๋Ÿฐ ์„น์…˜6] ๊ต์œก๊ณผ์ • ์„ค๊ณ„ (Javascript)

์€์ง„ 2021. 6. 27. 03:55

์ธํ”„๋Ÿฐ

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[์ธํ”„๋Ÿฐ ์„น์…˜6] ๊ต์œก๊ณผ์ • ์„ค๊ณ„ (Javascript)
์œ ๋ฃŒ ๊ฐ•์˜์ธ ๊ด€๊ณ„๋กœ ๋ฌธ์ œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.


โœ๏ธIdea Sketch

2021-06-27

1. ๋ฌด์กฐ๊ฑด c -> b -> a ์ˆœ์ด์–ด์•ผ ํ•œ๋‹ค

2. need๊ฐ€ ์•„๋‹Œ ๊ณผ๋ชฉ์€ ์ •๊ทœ์‹์œผ๋กœ ์ œ๊ฑฐํ•œ ํ›„, need์™€ ๋น„๊ตํ•˜๋ฉด?

plan = plan.replace(/[^CBA]/g, '');

๋ฌธ์ œ์ 
์ œ์™ธํ•  ๋ฌธ์ž์—ด๋กœ need ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค

3. need์™€ plan ๋‘˜ ๋‹ค queue๋กœ ์ƒ์„ฑ

  • while(plan.length > 0)
  • ํ•ญ์ƒ plan.shift()
  • plan.shift()์™€ need[0]์ด ๋™์ผํ•œ ๊ฒฝ์šฐ need.shift()
  • ๋งˆ์ง€๋ง‰์— need.length === 0 ์ด๋ฉด "YES", ์•„๋‹ˆ๋ฉด "NO" --> ์‚ผํ•ญ์—ฐ์‚ฐ์ž
while(plan.length) {
    if (plan.shift() === need[0]) need.shift();
}
return need.length ? "NO" : "YES";


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-27

function solution(need, plan) {
    need = need.split('');
    plan = plan.split('');

    while (plan.length) {
        if (plan.shift() === need[0]) need.shift();
    }

    return need.length ? "NO" : "YES";
}