Problem Solution/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JS] ๋ฐฐ์—ด์˜ ์œ ์‚ฌ๋„

yuri lee 2023. 9. 6. 22:38
๋ฐ˜์‘ํ˜•

Problem Description

๋‘ ๋ฐฐ์—ด์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€ ํ™•์ธํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฐฐ์—ด s1๊ณผ s2๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ™์€ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

Restrictions

  • 1 ≤ s1, s2์˜ ๊ธธ์ด ≤ 100
  • 1 ≤ s1, s2์˜ ์›์†Œ์˜ ๊ธธ์ด ≤ 10
  • s1๊ณผ s2์˜ ์›์†Œ๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค
  • s1๊ณผ s2๋Š” ๊ฐ๊ฐ ์ค‘๋ณต๋œ ์›์†Œ๋ฅผ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

Input/Output Example

  • ์ž…์ถœ๋ ฅ ์˜ˆ #1 "b"์™€ "c"๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ 2๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2 ๊ฐ™์€ ์›์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ 0์„ returnํ•ฉ๋‹ˆ๋‹ค.

 

My solution

function solution(s1, s2) {
    return s1.filter(el => s2.includes(el)).length
}

 

Another solutions

function solution(s1, s2) {
    var answer = 0;
    for (var i = 0; i < s1.length; i++){
        for (var j = 0; j < s2.length; j++){
            if(s1[i] === s2[j]) answer++;
        }
    }
    return answer;
}

https://school.programmers.co.kr/learn/courses/30/lessons/120903

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

๋ฐ˜์‘ํ˜•