๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿ–ฅ dev-ruby

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] [3์ฐจ]์••์ถ• - javascript <2018 KAKAO BLIND RECRUITMENT> ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] [3์ฐจ]์••์ถ• - javascript <2018 KAKAO BLIND RECRUITMENT>

ruby_s 2021. 11. 21. 18:12
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฌธ์ œ์„ค๋ช…

์‹ ์ž…์‚ฌ์› ์–ดํ”ผ์น˜๋Š” ์นด์นด์˜คํ†ก์œผ๋กœ ์ „์†ก๋˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์••์ถ•ํ•˜์—ฌ ์ „์†ก ํšจ์œจ์„ ๋†’์ด๋Š” ์—…๋ฌด๋ฅผ ๋งก๊ฒŒ ๋˜์—ˆ๋‹ค. ๋ฉ”์‹œ์ง€๋ฅผ ์••์ถ•ํ•˜๋”๋ผ๋„ ์ „๋‹ฌ๋˜๋Š” ์ •๋ณด๊ฐ€ ๋ฐ”๋€Œ์–ด์„œ๋Š” ์•ˆ ๋˜๋ฏ€๋กœ, ์••์ถ• ์ „์˜ ์ •๋ณด๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ๋ณต์› ๊ฐ€๋Šฅํ•œ ๋ฌด์†์‹ค ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

์–ดํ”ผ์น˜๋Š” ์—ฌ๋Ÿฌ ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์—์„œ ์„ฑ๋Šฅ์ด ์ข‹๊ณ  ๊ตฌํ˜„์ด ๊ฐ„๋‹จํ•œ LZW(Lempel–Ziv–Welch) ์••์ถ•์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. LZW ์••์ถ•์€ 1983๋…„ ๋ฐœํ‘œ๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ์ด๋ฏธ์ง€ ํŒŒ์ผ ํฌ๋งท์ธ GIF ๋“ฑ ๋‹ค์–‘ํ•œ ์‘์šฉ์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค.

LZW ์••์ถ•์€ ๋‹ค์Œ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

  1. ๊ธธ์ด๊ฐ€ 1์ธ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ํฌํ•จํ•˜๋„๋ก ์‚ฌ์ „์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  2. ์‚ฌ์ „์—์„œ ํ˜„์žฌ ์ž…๋ ฅ๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด w๋ฅผ ์ฐพ๋Š”๋‹ค.
  3. w์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์ „์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์ž…๋ ฅ์—์„œ w๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  4. ์ž…๋ ฅ์—์„œ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๋‹ค์Œ ๊ธ€์ž๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด(c), w+c์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์ „์— ๋“ฑ๋กํ•œ๋‹ค.
  5. ๋‹จ๊ณ„ 2๋กœ ๋Œ์•„๊ฐ„๋‹ค.

์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์˜๋ฌธ ๋Œ€๋ฌธ์ž๋งŒ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•  ๋•Œ, ์‚ฌ์ „์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ดˆ๊ธฐํ™”๋œ๋‹ค. ์‚ฌ์ „์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ๋Š” ์ •์ˆ˜๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง€๋ฉฐ, 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๊ณ  ํ•˜์ž.

์ƒ‰์ธ ๋ฒˆํ˜ธ 1 2 3 ... 24 25 26
๋‹จ์–ด A B C ... X Y Z

์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ์œผ๋กœ KAKAO๊ฐ€ ๋“ค์–ด์˜จ๋‹ค๊ณ  ํ•˜์ž.

  1. ํ˜„์žฌ ์‚ฌ์ „์—๋Š” KAKAO์˜ ์ฒซ ๊ธ€์ž K๋Š” ๋“ฑ๋ก๋˜์–ด ์žˆ์œผ๋‚˜, ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€์ธ KA๋Š” ์—†์œผ๋ฏ€๋กœ, ์ฒซ ๊ธ€์ž K์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์ธ ๋ฒˆํ˜ธ 11์„ ์ถœ๋ ฅํ•˜๊ณ , ๋‹ค์Œ ๊ธ€์ž์ธ A๋ฅผ ํฌํ•จํ•œ KA๋ฅผ ์‚ฌ์ „์— 27 ๋ฒˆ์งธ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  2. ๋‘ ๋ฒˆ์งธ ๊ธ€์ž A๋Š” ์‚ฌ์ „์— ์žˆ์œผ๋‚˜, ์„ธ ๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€์ธ AK๋Š” ์‚ฌ์ „์— ์—†์œผ๋ฏ€๋กœ, A์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ 1์„ ์ถœ๋ ฅํ•˜๊ณ , AK๋ฅผ ์‚ฌ์ „์— 28 ๋ฒˆ์งธ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  3. ์„ธ ๋ฒˆ์งธ ๊ธ€์ž์—์„œ ์‹œ์ž‘ํ•˜๋Š” KA๊ฐ€ ์‚ฌ์ „์— ์žˆ์œผ๋ฏ€๋กœ, KA์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์ธ ๋ฒˆํ˜ธ 27์„ ์ถœ๋ ฅํ•˜๊ณ , ๋‹ค์Œ ๊ธ€์ž O๋ฅผ ํฌํ•จํ•œ KAO๋ฅผ 29 ๋ฒˆ์งธ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  4. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๊ธ€์ž O์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์ธ ๋ฒˆํ˜ธ 15๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
ํ˜„์žฌ ์ž…๋ ฅ(w) ๋‹ค์Œ ๊ธ€์ž(c) ์ถœ๋ ฅ ์‚ฌ์ „ ์ถ”๊ฐ€(w+c)
K A 11 27: KA
A K 1 28: AK
KA O 27 29: KAO
O   15  

์ด ๊ณผ์ •์„ ๊ฑฐ์ณ ๋‹ค์„ฏ ๊ธ€์ž์˜ ๋ฌธ์žฅ KAKAO๊ฐ€ 4๊ฐœ์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ [11, 1, 27, 15]๋กœ ์••์ถ•๋œ๋‹ค.

์ž…๋ ฅ์œผ๋กœ TOBEORNOTTOBEORTOBEORNOT๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์••์ถ•์ด ์ง„ํ–‰๋œ๋‹ค.

ํ˜„์žฌ ์ž…๋ ฅ(w)๋‹ค์Œ ๊ธ€์ž(c)์ถœ๋ ฅ์‚ฌ์ „ ์ถ”๊ฐ€(w+c)

T O 20 27: TO
O B 15 28: OB
B E 2 29: BE
E O 5 30: EO
O R 15 31: OR
R N 18 32: RN
N O 14 33: NO
O T 15 34: OT
T T 20 35: TT
TO B 27 36: TOB
BE O 29 37: BEO
OR T 31 38: ORT
TOB E 36 39: TOBE
EO R 30 40: EOR
RN O 32 41: RNO
OT   34  

์ž…๋ ฅ ํ˜•์‹

์ž…๋ ฅ์œผ๋กœ ์˜๋ฌธ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด msg๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. msg์˜ ๊ธธ์ด๋Š” 1 ๊ธ€์ž ์ด์ƒ, 1000 ๊ธ€์ž ์ดํ•˜์ด๋‹ค.

์ถœ๋ ฅ ํ˜•์‹

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•œ ํ›„์˜ ์‚ฌ์ „ ์ƒ‰์ธ ๋ฒˆํ˜ธ๋ฅผ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅํ•˜๋ผ.

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

msg answer
KAKAO [11, 1, 27, 15]
TOBEORNOTTOBEORTOBEORNOT [20, 15, 2, 5, 15, 18, 14, 15, 20, 27, 29, 31, 36, 30, 32, 34]
ABABABABABABABAB [1, 2, 27, 29, 28, 31, 30]

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

function solution(msg) {
    const dictionary = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    const idxnum = [];
    while(msg.length !== 0){
        let w = "";
        for(let i=1; i<=msg.length; i++){
            const c = msg.substring(0, i);
            if(!dictionary.includes(c)){
                dictionary.push(c);
                break;
            }
            w = c;
        }
        idxnum.push(dictionary.indexOf(w)+1);
        msg = msg.substr(w.length);
    }
    return idxnum;
}

msg์—์„œ ํ˜„์žฌ ์ž…๋ ฅ์„ ๊ณ„์† ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋กœ์ง์„ ์งฐ๋‹ค.

1. ์ฃผ์–ด์ง„ ๋ฌธ์ œ์ฒ˜๋Ÿผ dictionary์— ๋ฏธ๋ฆฌ ์‚ฌ์ „์„ ์ •์˜ํ•ด ๋‘์—ˆ๋‹ค.

2. c์— substring๋กœ ๋ฌธ์ž์—ด์„ i๊นŒ์ง€ ์ž๋ฅธ ํ›„

  • ์‚ฌ์ „์— ๋“ฑ๋ก๋œ ๊ฒฝ์šฐ, ํ˜„์žฌ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„๋“ค์ด๊ณ ,
  • ๋“ฑ๋ก๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์‚ฌ์ „์— ๋“ฑ๋ก ํ›„, ์ด์ „์— w์— ๋Œ€์ž…๋˜์—ˆ๋˜ ํ˜„์žฌ ์ž…๋ ฅ์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ๋ฅผ idxnum์— ํ‘ธ์‹œํ•œ๋‹ค.

3. ์ถœ๋ ฅ์— ์ €์žฅํ•œ ํ˜„์žฌ ์ž…๋ ฅ์„ msg์—์„œ ์ œ๊ฑฐํ•˜์—ฌ ์—…๋ฐ์ดํŠธ ์‹œํ‚จ๋‹ค.

substring๊ณผ substr์˜ ์ฐจ์ด๋ฅผ ํ™•์‹คํžˆ ์•Œ์•„๊ฐ„ ๋ฌธ์ œ.
728x90
๋ฐ˜์‘ํ˜•
LIST