๐ง์๊ณ ๋ฆฌ์ฆ?์๊ณ ์ถ์!
ํ๋ก๊ทธ๋๋จธ์ค (์๋ฐ์คํฌ๋ฆฝํธ) - ์ธ๊ณ์ด ์ฌ์
eazyseon
2023. 3. 2. 16:35
๋ฐ์ํ
- ๋ฌธ์ ์ค๋ช
- ์ ์ถ๋ ฅ ์์
- ๋์ ํ์ด
function solution(spell, dic) {
let cnt = 0;
let answer = []
//dic ๋ฐ๋ณต๋ฌธ
for(let i=0; i<dic.length; i++){
cnt = 0;
//spell์ ๊ธธ์ด๋งํผ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉฐ
for(let j=0; j<spell.length; j++){
//dic[i]์์ spell[j]์ ์์๊ฐ ์๋์ง ํ์ธ
if(dic[i].includes(spell[j])){
//์๋ค๋ฉด cnt๋ฅผ ++ํ๋ค
cnt++;
}
}
//cnt์ spell์ ๊ธธ์ด๊ฐ ๊ฐ๋ค๋ฉด
if(cnt === spell.length){
//answer์์ ๋ฃ๋๋ค
answer.push(dic[i])
}
}
//answer์์ ์๋ฌด๊ฒ๋ ์๋ค๋ฉด(๊ธธ์ด๊ฐ 0์ด๋ฉด)
//ํด๋น๋๋ ๋จ์ด๊ฐ ์๋ค๋ ๋ป์ด๋ 2๋ฅผ ๋ฐํ
//์๋ค๋ฉด(๊ธธ์ด๊ฐ 1์ด๋ฉด) 1์ ๋ฐํ
return answer.length === 0? 2: 1;
}
- ๋ค๋ฅธ ์ฌ๋์ ํ์ด
function solution(spell, dic) {
return dic.filter(v=>spell.every(c=>v.includes(c))).length ? 1 : 2;
}
๋ฐ์ํ