๐Ÿช“[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ

Chobbyยท2022๋…„ 12์›” 15์ผ
1

Programmers

๋ชฉ๋ก ๋ณด๊ธฐ
153/349
post-custom-banner

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

๋ฌธ์ž์—ด s๊ฐ€ ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ ๋‹ค์Œ ๊ทœ์น™์„ ๋”ฐ๋ผ์„œ ์ด ๋ฌธ์ž์—ด์„ ์—ฌ๋Ÿฌ ๋ฌธ์ž์—ด๋กœ ๋ถ„ํ•ดํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋จผ์ € ์ฒซ ๊ธ€์ž๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค. ์ด ๊ธ€์ž๋ฅผ x๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค.
์ด์ œ ์ด ๋ฌธ์ž์—ด์„ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ฝ์–ด๋‚˜๊ฐ€๋ฉด์„œ, x์™€ x๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ธ€์ž๋“ค์ด ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผ ๊ฐ๊ฐ ์…‰๋‹ˆ๋‹ค. ์ฒ˜์Œ์œผ๋กœ ๋‘ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™์•„์ง€๋Š” ์ˆœ๊ฐ„ ๋ฉˆ์ถ”๊ณ , ์ง€๊ธˆ๊นŒ์ง€ ์ฝ์€ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
s์—์„œ ๋ถ„๋ฆฌํ•œ ๋ฌธ์ž์—ด์„ ๋นผ๊ณ  ๋‚จ์€ ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๋‚จ์€ ๋ถ€๋ถ„์ด ์—†๋‹ค๋ฉด ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ๋‘ ํšŸ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ์ƒํƒœ์—์„œ ๋” ์ด์ƒ ์ฝ์„ ๊ธ€์ž๊ฐ€ ์—†๋‹ค๋ฉด, ์—ญ์‹œ ์ง€๊ธˆ๊นŒ์ง€ ์ฝ์€ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜๊ณ , ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์œ„ ๊ณผ์ •๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด๋“ค๋กœ ๋ถ„ํ•ดํ•˜๊ณ , ๋ถ„ํ•ดํ•œ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋Š” ํ•จ์ˆ˜ solution์„ ์™„์„ฑํ•˜์„ธ์š”.


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

  • 1 โ‰ค s์˜ ๊ธธ์ด โ‰ค 10,000
  • s๋Š” ์˜์–ด ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

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

sresult
"banana"3
"abracadabra"6
"aaabbaccccabba"3

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

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

  • s="banana"์ธ ๊ฒฝ์šฐ ba - na - na์™€ ๊ฐ™์ด ๋ถ„ํ•ด๋ฉ๋‹ˆ๋‹ค.

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

  • s="abracadabra"์ธ ๊ฒฝ์šฐ ab - ra - ca - da - br - a์™€ ๊ฐ™์ด ๋ถ„ํ•ด๋ฉ๋‹ˆ๋‹ค.

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

  • s="aaabbaccccabba"์ธ ๊ฒฝ์šฐ aaabbacc - ccab - ba์™€ ๊ฐ™์ด ๋ถ„ํ•ด๋ฉ๋‹ˆ๋‹ค.

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

function solution(s) {
    let isX = [s[0],0]
    let isNotX = ''
    const result = []
    for(let i = 0 ; i < s.length ; i ++) {
        // X ๊ฐ€ ์ด๋ฒˆ ๊ธ€์ž์ธ์ง€ ์ฒ˜๋ฆฌ
        if(isX[0] === s[i]) isX[1]++
        else isNotX+=s[i]
        // X์™€ X๊ฐ€ ์•„๋‹Œ ๊ธ€์ž๋“ค์˜ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด
        if(isX[1] === isNotX.length) {
            // ์ •๋‹ต ๋ฐฐ์—ด์— ์ถ”๊ฐ€ ๋ฐ ์ดˆ๊ธฐํ™”
            result.push(isX[0]+isNotX)
            isX = [s[i+1],0]
            isNotX = ''
        }
    }
    // X๊ฐ€ ๋‚จ์•˜๋‹ค๋ฉด ์ •๋‹ต ๋ฐฐ์—ด์— ์ถ”๊ฐ€
    if(isX[0]) result.push(isX[0])
    return result.length
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ
post-custom-banner

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