이것이 취업을 위한 코딩테스트다. with 파이썬 p.323
def solution(s):
result=9999
if len(s)==1:
result=1
for step in range(1,len(s)//2+1):
compressed = ""
prev=s[0:step]
count=1
for i in range(step, len(s), step):
if prev==s[i:i+step]:
count+=1
else:
if count > 1:
compressed += str(count)+prev
else:
compressed += prev
prev = s[i:i+step]
count=1
if count > 1:
compressed += str(count)+prev
else:
compressed += prev
result = min(result, len(compressed))
return result
테스트 케이스 : "a" 일 때, 길이가 1이 나와야한다.
[if문 줄이기]
def solution(s):
result=9999
if len(s)==1:
result=1
for step in range(1,len(s)//2+1):
compressed = ""
prev=s[0:step]
count=1
for i in range(step, len(s), step):
if prev==s[i:i+step]:
count+=1
else:
compressed += str(count)+prev if count > 1 else prev
prev = s[i:i+step]
count=1
compressed += str(count)+prev if count > 1 else prev
result = min(result, len(compressed))
return result
댓글