
https://programmers.co.kr/learn/courses/30/lessons/60057
def solution(s):
# 모든 문자가 같은 경우를 처리
temp = s[0]
allSame = True
for i in s:
if temp != i:
allSame = False
if allSame == True:
if len(s) == 1:
return 1
else:
return 2
answer = 1001 # 최대 길이로 설정
# 길이로 잘라서 배열 형성
piece = len(s)//2
while piece > 0:
sum = 0
index = 0
array = []
while index + piece <= len(s):
array.append(s[index:index + piece])
index += piece
if index != len(s):
array.append(s[index:])
# 전체 길이를 계산
while array:
same = False
count = 1
a = array.pop(0)
for i in array:
if i == a:
count += 1
same = True
else:
break
if same == True:
for i in range(count - 1):
array.pop(0)
digit = 0
while count//10 != 0:
digit += 1
count //= 10
sum += piece + 1 + digit
else:
sum += len(a)
if sum != 0 and sum < len(s) and answer > sum:
answer = sum
piece -= 1
# 압축할 수 있는 문자열이 없다면 길이를 반환
if answer > len(s):
answer = len(s)
return answer
문제 푸는 시간이 너무 길었다.
2시간 정도 걸린듯.
테스트 케이스를 모두 통과하고서 제출했는데 오답이 나왔었다.
질문하기에서 오답 케이스를 찾아보니 같은 문자가 10번 이상 반복된 경우에 대한 처리가 빠져있었다.
시간이 오래걸렸지만 그래도 제대로 된 답을 제출할 수 있었다.
시간이 너무 오래 걸린다면 포기하고 넘기기로 했는데
조금만 더 하면 풀 수 있을거라는 생각에 포기를 하지 못하고 끝까지 물고 늘어져버렸다.
경험이 쌓이면 풀이 시간도 짧아지고 오답도 줄것이라고 생각한다.