ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

def solution(s):
    # ์ตœ์†Œ ์••์ถ•๊ฐ’์„ ์ฒ˜์Œ์—๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋กœ ์ดˆ๊ธฐํ™” (์ตœ๋Œ€๋กœ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์ด๋‹ˆ๊นŒ)
    answer = len(s)

    for unit in range(len(s)):
        print("===================================")
        unit += 1 # ์••์ถ• ๋‹จ์œ„์ด๋‹ˆ๊นŒ 1์„ ๋”ํ•จ
        print("๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : {}".format(unit))
        count = 1 # ๋ฐ˜๋ณต๋˜๋Š” ํšŸ์ˆ˜
        result = "" # ์••์ถ•๋œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด, ๋‹จ์œ„๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”

        compression_unit = s[:unit] # ์••์ถ• ๋ฌธ์ž์—ด ์ €์žฅ
        print("์••์ถ• ๋ฌธ์ž์—ด : {}".format(compression_unit))

        for i in range(unit, len(s), unit):
            # ๋น„๊ต ๋ฌธ์ž <- s[i:i+unit]
            print("๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : {} ??? ์••์ถ• ๋ฌธ์ž์—ด : {}".format(s[i:i+unit], compression_unit))

            # ์••์ถ•๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ต๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ตํ•œ๋‹ค
            if s[i:i+unit] == compression_unit:
                print("๊ฐ™๋„ค! count ์ฆ๊ฐ€")
                count += 1
            else:
                print("๋‹ค๋ฅด๋„ค!")
                result += str(count) + compression_unit
                print("result = {}".format(result))
                compression_unit = s[i:i+unit]
                count = 1

        print("์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž")
        print("answer {} ??? result {}".format(answer, len(result)))
        if answer > len(result):
            print("์••์ถ•๋ฅ ์ด ๋” ์ž‘์€ ๋†ˆ์„ ์ฐพ์Œ")
            answer = len(result)
        print("answer : {}\n".format(answer))
    return answer


s="aabbaccc"

print(solution(s))

 

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 1
์••์ถ• ๋ฌธ์ž์—ด : a
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : a ??? ์••์ถ• ๋ฌธ์ž์—ด : a
๊ฐ™๋„ค! count ์ฆ๊ฐ€
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : b ??? ์••์ถ• ๋ฌธ์ž์—ด : a
๋‹ค๋ฅด๋„ค!
result = 2a
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : b ??? ์••์ถ• ๋ฌธ์ž์—ด : b
๊ฐ™๋„ค! count ์ฆ๊ฐ€
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : a ??? ์••์ถ• ๋ฌธ์ž์—ด : b
๋‹ค๋ฅด๋„ค!
result = 2a2b
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : c ??? ์••์ถ• ๋ฌธ์ž์—ด : a
๋‹ค๋ฅด๋„ค!
result = 2a2b1a
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : c ??? ์••์ถ• ๋ฌธ์ž์—ด : c
๊ฐ™๋„ค! count ์ฆ๊ฐ€
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : c ??? ์••์ถ• ๋ฌธ์ž์—ด : c
๊ฐ™๋„ค! count ์ฆ๊ฐ€
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 8 ??? result 6
์••์ถ•๋ฅ ์ด ๋” ์ž‘์€ ๋†ˆ์„ ์ฐพ์Œ
answer : 6

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 2
์••์ถ• ๋ฌธ์ž์—ด : aa
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : bb ??? ์••์ถ• ๋ฌธ์ž์—ด : aa
๋‹ค๋ฅด๋„ค!
result = 1aa
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : ac ??? ์••์ถ• ๋ฌธ์ž์—ด : bb
๋‹ค๋ฅด๋„ค!
result = 1aa1bb
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : cc ??? ์••์ถ• ๋ฌธ์ž์—ด : ac
๋‹ค๋ฅด๋„ค!
result = 1aa1bb1ac
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 6 ??? result 9
answer : 6

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 3
์••์ถ• ๋ฌธ์ž์—ด : aab
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : bac ??? ์••์ถ• ๋ฌธ์ž์—ด : aab
๋‹ค๋ฅด๋„ค!
result = 1aab
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : cc ??? ์••์ถ• ๋ฌธ์ž์—ด : bac
๋‹ค๋ฅด๋„ค!
result = 1aab1bac
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 6 ??? result 8
answer : 6

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 4
์••์ถ• ๋ฌธ์ž์—ด : aabb
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : accc ??? ์••์ถ• ๋ฌธ์ž์—ด : aabb
๋‹ค๋ฅด๋„ค!
result = 1aabb
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 6 ??? result 5
์••์ถ•๋ฅ ์ด ๋” ์ž‘์€ ๋†ˆ์„ ์ฐพ์Œ
answer : 5

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 5
์••์ถ• ๋ฌธ์ž์—ด : aabba
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : ccc ??? ์••์ถ• ๋ฌธ์ž์—ด : aabba
๋‹ค๋ฅด๋„ค!
result = 1aabba
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 5 ??? result 6
answer : 5

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 6
์••์ถ• ๋ฌธ์ž์—ด : aabbac
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : cc ??? ์••์ถ• ๋ฌธ์ž์—ด : aabbac
๋‹ค๋ฅด๋„ค!
result = 1aabbac
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 5 ??? result 7
answer : 5

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 7
์••์ถ• ๋ฌธ์ž์—ด : aabbacc
๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : c ??? ์••์ถ• ๋ฌธ์ž์—ด : aabbacc
๋‹ค๋ฅด๋„ค!
result = 1aabbacc
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 5 ??? result 8
answer : 5

===================================
๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : 8
์••์ถ• ๋ฌธ์ž์—ด : aabbaccc
์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž
answer 5 ??? result 0
์••์ถ•๋ฅ ์ด ๋” ์ž‘์€ ๋†ˆ์„ ์ฐพ์Œ
answer : 0

0

Process finished with exit code 0

 

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์„ ๋•Œ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์ ์€ 1๋„ ๋”ธ๋ ค๊ฐ€์„œ ๋ถ™๋Š”๋‹ค๋Š” ๊ฒƒ๊ณผ ๋’ค์— ๋‚จ์€ ๋ฌธ์ž์—ด์€ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋˜์—ˆ๋‹ค๋Š” ์ ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ 7์ธ๋ฐ ์••์ถ•๋‹จ์œ„ 8๊นŒ์ง€ ๊ตฌํ•˜๋ฉด ์•ˆ๋ผ! ์ฒ˜์Œ ๋ฐ˜๋ณต๋ฌธ ๋ฒ”์œ„ len(s)-1๋กœ ์ˆ˜์ •ํ•ด์ฃผ์–ด์•ผํ•จ, ๋‘๋ฒˆ์งธ for๋ฌธ์—์„œ๋Š” ์ข…๋ฃŒ๋ฒ”์œ„๋ฅผ len(s)+1๋กœ ํ•ด์ฃผ์–ด์•ผํ•จ

 

def solution(s):
    # ์ตœ์†Œ ์••์ถ•๊ฐ’์„ ์ฒ˜์Œ์—๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋กœ ์ดˆ๊ธฐํ™” (์ตœ๋Œ€๋กœ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์ด๋‹ˆ๊นŒ)
    answer = len(s)

    for unit in range(len(s)-1):
        print("===================================")
        unit += 1 # ์••์ถ• ๋‹จ์œ„์ด๋‹ˆ๊นŒ 1์„ ๋”ํ•จ
        print("๋ฌธ์ž์—ด ์••์ถ• ๋‹จ์œ„ : {}".format(unit))
        count = 1 # ๋ฐ˜๋ณต๋˜๋Š” ํšŸ์ˆ˜
        result = "" # ์••์ถ•๋œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด, ๋‹จ์œ„๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”

        compression_unit = s[:unit] # ์••์ถ• ๋ฌธ์ž์—ด ์ €์žฅ
        print("์••์ถ• ๋ฌธ์ž์—ด : {}".format(compression_unit))

        for i in range(unit, len(s)+1, unit):
            print("i๊ฐ€ {}์ผ ๋•Œ".format(i))
            # ๋น„๊ต ๋ฌธ์ž <- s[i:i+unit]
            print("๋น„๊ต๋‹นํ•  ๋ฌธ์ž์—ด : {} ??? ์••์ถ• ๋ฌธ์ž์—ด : {}".format(s[i:i+unit], compression_unit))

            # ์••์ถ•๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ต๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ตํ•œ๋‹ค
            if s[i:i+unit] == compression_unit:
                print("๊ฐ™๋„ค! count ์ฆ๊ฐ€")
                count += 1
            else:
                print("๋‹ค๋ฅด๋„ค!")
                result += str(count) + compression_unit if count>1 else compression_unit # 1 ๋ถ™์ด๋Š”๊ฑฐ ํ•ด๊ฒฐ
                print("result = {}".format(result))
                compression_unit = s[i:i+unit]
                count = 1
        print("i์˜ ๊ฐ’ {}".format(i))
        result += s[i+1:]

        print("์••์ถ•ํ•œ ๊ฒฐ๊ณผ : {}".format(result))
        print("์ตœ์†Œ๊ฐ’ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž")
        print("answer {} ??? result {}".format(answer, len(result)))
        if answer > len(result):
            print("์••์ถ•๋ฅ ์ด ๋” ์ž‘์€ ๋†ˆ์„ ์ฐพ์Œ")
            answer = len(result)
        print("answer : {}\n".format(answer))
    return answer

 

์™œ์ฃ ?๋ถ€๋“ค๋ถ€๋“ค

print("i์˜ ๊ฐ’ : {}".format(i))
        result += s[i:]

๋‘ ๋ฒˆ์งธ for๋ฌธ์ด ๋๋‚˜๊ณ  ๋ง๋ถ™์—ฌ์ง€๋Š” ๋ฌธ์ž์—ด ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟจ๋”๋‹ˆ

ํ†ต๊ณผ ์˜ˆ์ด์˜ˆ

ํ‘ํ‘ ๋ช‡์‹œ๊ฐ„ ๊ฑธ๋ ธ์ง€..? ํ’€๊ฒŒ ๋งŽ์€๋ฐ ์ด ๋ฌธ์ œ์—๋งŒ ๋ช‡์‹œ๊ฐ„์„ ์Ÿ์€๊ฑด์ง€ใ… ใ… 

 

 

 

 

์ฝ”๋“œ ๋ฆฌ๋ทฐ

  • ์—ฌ๋Ÿฌ ๋ฒˆ ์“ฐ์ด๋Š” ๊ฐ’์€ ๋ณ€์ˆ˜์— ๋‹ด๋Š”๋‹ค.
  • ์ตœ์†Œ๊ฐ’ min() ํ•จ์ˆ˜

 

์ˆ˜์ • ์ฝ”๋“œ

def solution(s):
    # ์ตœ์†Œ ์••์ถ•๊ฐ’์„ ์ฒ˜์Œ์—๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋กœ ์ดˆ๊ธฐํ™” (์ตœ๋Œ€๋กœ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์ด๋‹ˆ๊นŒ)
    answer = len(s)

    for unit in range(len(s)-1):
        unit += 1 # ์••์ถ• ๋‹จ์œ„์ด๋‹ˆ๊นŒ 1์„ ๋”ํ•จ
        count = 1 # ๋ฐ˜๋ณต๋˜๋Š” ํšŸ์ˆ˜
        result = "" # ์••์ถ•๋œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด, ๋‹จ์œ„๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”

        compression_unit = s[:unit] # ์••์ถ• ๋ฌธ์ž์—ด ์ €์žฅ

        for i in range(unit, len(s)+1, unit):
            # ๋น„๊ต ๋ฌธ์ž <- s[i:i+unit]
            # ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์€ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
            compare_word = s[i:i+unit]

            # ์••์ถ•๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ต๋ฌธ์ž์—ด์ด๋ž‘ ๋น„๊ตํ•œ๋‹ค
            if compare_word == compression_unit:
                count += 1
            else:
                result += str(count) + compression_unit if count>1 else compression_unit # 1 ๋ถ™์ด๋Š”๊ฑฐ ํ•ด๊ฒฐ
                compression_unit = compare_word
                count = 1

        # ๋‚จ์€ ๋ฌธ์ž ๋์— ๋ถ™์—ฌ์ฃผ๊ธฐ
        result += s[i:]

        # min() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค.
        # if answer > len(result):
        #     answer = len(result)
        answer = min(answer, len(result))

    return answer
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€