Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (1์ฐจ) ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง (js) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (1์ฐจ) ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง (js)

Lennon 2021. 10. 27. 18:23
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/17677?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง

๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง ์—ฌ๋Ÿฌ ์–ธ๋ก ์‚ฌ์—์„œ ์Ÿ์•„์ง€๋Š” ๋‰ด์Šค, ํŠนํžˆ ์†๋ณด์„ฑ ๋‰ด์Šค๋ฅผ ๋ณด๋ฉด ๋น„์Šท๋น„์Šทํ•œ ์ œ๋ชฉ์˜ ๊ธฐ์‚ฌ๊ฐ€ ๋งŽ์•„ ์ •์ž‘ ํ•„์š”ํ•œ ๊ธฐ์‚ฌ๋ฅผ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค. Daum ๋‰ด์Šค์˜ ๊ฐœ๋ฐœ ์—…๋ฌด๋ฅผ ๋งก๊ฒŒ ๋œ ์‹ ์ž…์‚ฌ์› ํŠœ๋ธŒ

programmers.co.kr

 

function solution(str1, str2) {
    str1 = divideString(str1).map((v) => v.join("").toLowerCase());
    str2 = divideString(str2).map((v) => v.join("").toLowerCase());
    
    let denominatorSet = [...new Set([...str1, ...str2])];
    let moleculeSet = str1.filter(v => str2.includes(v));
    moleculeSet = [...new Set(moleculeSet)];
    
    let molecule = 0;
    let denominator = 0;
    
    for(let i = 0; i < denominatorSet.length; i++){
        const strOverlap1 = str1.reduce((prev, cur) => prev + (cur===denominatorSet[i]), 0);
        const strOverlap2 = str2.reduce((prev, cur) => prev + (cur===denominatorSet[i]), 0);
        denominator+= Math.max(strOverlap1, strOverlap2);
    }
    for(let i = 0; i < moleculeSet.length; i++){
        const strOverlap1 = str1.reduce((prev, cur) => prev + (cur===moleculeSet[i]), 0);
        const strOverlap2 = str2.reduce((prev, cur) => prev + (cur===moleculeSet[i]), 0); 
        
        molecule+= Math.min(strOverlap1, strOverlap2);
    }
    return denominator===0 ? 65536 : Math.floor((molecule/denominator)*65536)

}

function divideString(str) {
    const result = [];
    
    for(let i = 0; i < str.length; i++){
        const sliceStr = str.slice(i,i+2);
        let matchStr = sliceStr.match(/[A-Z]/gi);
        if(matchStr === null) continue;
        else if(matchStr.length === 2){
            result.push(matchStr);
        }
    }
    return result;
}

 

matchํ•จ์ˆ˜๋ฅผ ๋ฐฐ์šด ํ›„ ์ฒ˜์Œ ์จ ๋ดค๋‹ค. 

 

๋ฐฐ์—ด ์†์—์„œ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฑฐ์— ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„ divideString ํ•จ์ˆ˜์— ์ž‘์„ฑํ•ด๋ดค๋‹ค.

์ •๊ทœ์‹์„ ํ†ตํ•ด ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์งˆ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments