๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Javascript] ์™ธ๊ณ„์–ด ์‚ฌ์ „ || ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

728x90

๋ฌธ์ œ๊ฐ€ ์ž˜๋ชป๋๋‹ค. 

" spell์— ๋‹ด๊ธด ์•ŒํŒŒ๋ฒณ์„ ํ•œ๋ฒˆ์”ฉ๋งŒ ๋ชจ๋‘ ์‚ฌ์šฉํ•œ ๋‹จ์–ด๊ฐ€ dic์— ์กด์žฌํ•œ๋‹ค๋ฉด 1, ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด 2๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”." ๋ผ๊ณ  ์จ์žˆ๋Š”๋ฐ, 

 

ํฌํ•จ๋œ ๋‹จ์–ด๋ฅผ ํ•œ๋ฒˆ ์ด์ƒ ์‚ฌ์šฉํ•œ ๋‹จ์–ด๋„ ํ†ต๊ณผ๋จ. 

spell์— ๋‹ด๊ธด ์•ŒํŒŒ๋ฒณ์„ ํ•œ๋ฒˆ ์ด์ƒ ๋ชจ๋‘ ์‚ฌ์šฉ 

ํ˜น์€ 

spell์— ๋‹ด๊ธด ์•ŒํŒŒ๋ฒณ์ด ๋ชจ๋‘ ํฌํ•จ๋œ ๋‹จ์–ด 

๋ผ๊ณ  ์“ฐ์—ฌ์ ธ์•ผ ํ•œ๋‹ค.

 

 

 

 

<๋‚˜์˜ ํ’€์ด>

์ฒ˜์Œ์— ํ•œ๋ฒˆ์”ฉ๋งŒ ๋“ค์–ด๊ฐ€์•ผํ•˜๋Š” ์ค„ ์•Œ์•„์„œ ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน์—ˆ๋‹ค..๐Ÿ˜ 

function solution(spell, dic) {
    let notMatchWords = [];
    dic.map(word => {
        let notMatch = 0;
        let wordArr = word.split("").map(v => v);
        spell.forEach(v => { wordArr.includes(v) || notMatch++ });
        if (notMatch === 0) {
            notMatchWords.push(word);
        }
    })
    if (notMatchWords.length > 0) {
        return 1;
    } else {
        return 2;
    }
}

 

 

 

 

 

<๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด>

function solution(p, d) {
    return d.some(s => p.sort().toString() == [...s].sort().toString()) ? 1 : 2;
}

์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ด์„œ ๋น„๊ตํ•œ ํ›„, ๊ฒฐ๊ณผ๊ฐ€ true ๋ฉด 1์„, false ๋ฉด 2๋ฅผ return ํ•œ๋‹ค. 

๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ํ•จ์ˆ˜. 

 

 

 

function solution(spell, dic) {
    return dic.filter(v=>spell.every(c=>v.includes(c))).length ? 1 : 2;
}

๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํŒ๋ณ„ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๋Š” ํ•จ์ˆ˜. 

728x90
๋ฐ˜์‘ํ˜•