post-custom-banner

๐Ÿงก๋ฌธ์ œ ์„ค๋ช…

์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด t์™€ p๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, t์—์„œ p์™€ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋ถ€๋ถ„๋ฌธ์ž์—ด ์ค‘์—์„œ, ์ด ๋ถ€๋ถ„๋ฌธ์ž์—ด์ด ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜๊ฐ€ p๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒƒ์ด ๋‚˜์˜ค๋Š” ํšŸ์ˆ˜๋ฅผ returnํ•˜๋Š” ํ•จ์ˆ˜ solution์„ ์™„์„ฑํ•˜์„ธ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด, t="3141592"์ด๊ณ  p="271" ์ธ ๊ฒฝ์šฐ, t์˜ ๊ธธ์ด๊ฐ€ 3์ธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ 314, 141, 415, 159, 592์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ž์—ด์ด ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜ ์ค‘ 271๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜๋Š” 141, 159 2๊ฐœ ์ž…๋‹ˆ๋‹ค.


๐Ÿ’›์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค p์˜ ๊ธธ์ด โ‰ค 18
  • p์˜ ๊ธธ์ด โ‰ค t์˜ ๊ธธ์ด โ‰ค 10,000
  • t์™€ p๋Š” ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด๋ฉฐ, 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๐Ÿ’š์ž…์ถœ๋ ฅ ์˜ˆ

tpresult
"3141592""271"2
"500220839878""7"8
"10203""15"3

๐Ÿ’™์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ณธ๋ฌธ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • p์˜ ๊ธธ์ด๊ฐ€ 1์ด๋ฏ€๋กœ t์˜ ๋ถ€๋ถ„๋ฌธ์ž์—ด์€ "5", "0", 0", "2", "2", "0", "8", "3", "9", "8", "7", "8"์ด๋ฉฐ ์ด์ค‘ 7๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž๋Š” "5", "0", "0", "2", "2", "0", "3", "7" ์ด๋ ‡๊ฒŒ 8๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • p์˜ ๊ธธ์ด๊ฐ€ 2์ด๋ฏ€๋กœ t์˜ ๋ถ€๋ถ„๋ฌธ์ž์—ด์€ "10", "02", "20", "03"์ด๋ฉฐ, ์ด์ค‘ 15๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž๋Š” "10", "02", "03" ์ด๋ ‡๊ฒŒ 3๊ฐœ์ž…๋‹ˆ๋‹ค. "02"์™€ "03"์€ ๊ฐ๊ฐ 2, 3์— ํ•ด๋‹นํ•œ๋‹ค๋Š” ์ ์— ์ฃผ์˜ํ•˜์„ธ์š”

๐Ÿ’œ๋‚˜์˜ ํ’€์ด

function solution(t, p) {
    // t ๋ฌธ์ž์—ด ์ˆœํšŒ
    return [...t].map((a, i, origin) => {
        // t ๋ฌธ์ž์—ด์„ p ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋กœ ์ž๋ฅด๊ธฐ
        const curStr = origin.slice(i, i+p.length)
        if(curStr.length === p.length) {
            // ๋ฌธ์ž์—ด์˜ ๊ฐ’์ด p ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ์ธ๊ฐ€?
            return Number(curStr.join("")) <= Number(p)
        }
        // t ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ p์™€ ๋‹ค๋ฅด๋‹ค๋ฉด null ๋ฐ˜ํ™˜
        return null
    }).reduce((acc, cur, idx) => cur ? acc+1 : acc , 0)
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€