Problem Solution/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JS] ๋ฌธ์ž์—ด์•ˆ์— ๋ฌธ์ž์—ด

yuri lee 2023. 9. 5. 20:36
๋ฐ˜์‘ํ˜•

Problem Description

์ •์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ n์˜ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์˜ ํ•ฉ์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”

 

Restrictions

  • 0 ≤ n ≤ 1,000,000

 

Input/Output Example

  • ์ž…์ถœ๋ ฅ ์˜ˆ #1 1 + 2 + 3 + 4 = 10์„ returnํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2 9 + 3 + 0 + 2 + 1 + 1 = 16์„ returnํ•ฉ๋‹ˆ๋‹ค.

 

My solution

function solution(str1, str2) {
    return str1.includes(str2) ? 1 : 2
}

 

Another solutions

function solution(str1, str2) {
    return str1.split(str2).length > 1 ? 1 : 2
}

 

str1
'123AB123'
str1.split(str2)
(2) ['123', '123']
['123', '123'].length


https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

 

Array.prototype.includes() - JavaScript | MDN

includes() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์ด ํŠน์ • ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

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

 

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

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

programmers.co.kr

 

๋ฐ˜์‘ํ˜•