Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ตฌํ˜„ - ๋ฌธ์ž์—ด ์••์ถ•

๊ฐ์ž ๐Ÿฅ” 2021. 9. 1. 18:46
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฌธ์ž์—ด ์••์ถ•

๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ "์–ดํ”ผ์น˜"๋Š” ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ๋น„์†์‹ค ์••์ถ• ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฌธ

programmers.co.kr

 

๋‚˜์˜ ํ’€์ด

- ๊ฑธ๋ฆฐ์‹œ๊ฐ„ : 1์‹œ๊ฐ„

  1. ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์˜ ์ ˆ๋ฐ˜๊ธธ์ด๋งŒํผ ์••์ถ•์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ len(s)//2 ๊นŒ์ง€๋งŒ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆผ
  2. ์•ž์—์„œ ๋ถ€ํ„ฐ num ๋งŒํผ ์งœ๋ฅธ ๋‹จ์–ด๋ฅผ now๋กœ ๋†“๊ณ , now๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋น„๊ต๋ฅผ ์‹ค์‹œ
  3. now ๋’ค์— ๊ธธ์ด num๋งŒํผ ์งœ๋ฅธ ๋‹จ์–ด๋ฅผ nex๋ผ๊ณ  ์ง€์ •
  4. now์™€ nex๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ result์— ์—ฐ๊ฒฐ์‹œ์ผœ์คŒ
  5. 2๋ฒˆ ์ด์ƒ ๋“ฑ์žฅํ• ๋•Œ๋งŒ ์ˆซ์ž๋ฅผ ์ ์–ด์ฃผ๋ฏ€๋กœ if๋ฌธ์„ ํ™œ์šฉํ•ด์„œ count ๊ฐ’์„ 2์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๊ณ , ์•„๋‹๊ฒฝ์šฐ ๊ทธ๋ƒฅ ๋ฌธ์ž์—ด์„ ๋ถ™์—ฌ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค. 
def solution(s):
    
    answer = len(s)
    
    for num in range(1, len(s)//2+1):
        result = ''
        count =1
        # ์ง€๊ธˆ ๋น„๊ตํ•  now 
        now = s[:num]
        for i in range(num, len(s), num):
            # now ๋’ค์—์žˆ๋Š” ๋ฌธ์ž์—ด nex 
            nex = s[i:i+num]
            if now == nex:
                count+=1
            else:
                if count >= 2: # 2์ด์ƒ์ด์–ด์•ผ ์ˆซ์ž ์ ์Œ, 1์€ ์ƒ๋žต
                    result += str(count) + now
                else:
                    result += now
                now = nex #now๋ฅผ nex๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ณผ์ • 
                count = 1
        # ๋‹ค๋Œ์•„๊ฐ€๊ณ  ๋‚˜๋จธ์ง€
        if count >= 2:
            result += str(count) + now
        else:
            result += now
        answer = min(answer, len(result))
        result = '' #์ดˆ๊ธฐํ™”

    return answer

 

๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์กฐ๊ธˆ ํ—ค๋งค๊ธฐ๋„ ํ–ˆ๊ณ , ์‹œ๊ฐ„์ด ์กฐ๊ธˆ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค. ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•๊ณผ for๋ฌธ์„ ๋‹ค ๋Œ์•„๊ฐ€๊ณ  ๋งˆ์ง€๋ง‰์— ๋„ฃ์–ด์ฃผ๋Š” ๊ณผ์ •์—์„œ ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„์ด ๋งŽ์•˜๋‹ค. ๋‹ค๋ฅธ์‚ฌ๋žŒํ’€์ด์— ํ›Œ๋ฅญํ•œ ํ’€์ด๊ฐ€ ์žˆ์œผ๋‹ˆ ์ข‹์•„์š” ์ˆœ์œผ๋กœ 2๊ฐœ ์ •๋„ ์ •๋…ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋ณด์ž.

 

๋ฐ˜์‘ํ˜•