#output string에 문자랑, 압축된 숫자를 바로 add
def compressWord(inputString):
buffer = None
result = ''
cnt = 1
for ch in inputString:
if buffer is None:
result += ch
buffer = ch
else:
if buffer is ch:
cnt += 1
else:
result += str(cnt)
cnt = 1
result += ch
buffer = ch
result += str(cnt)
return result
if __name__ == '__main__':
result=compressWord("aaaaabbcdddaaa")
print(result) #output : a5b2c1d3a3
반응형