python 문자열 압축하기
조회수 896회
2 답변
-
요소의 개수를 세고, 문자열로 나열하는 문제입니다.
파이썬 내장 라이브러리를 활용하여 풀면 다음과 같습니다.
from collections import Counter from itertools import chain def compress(value): counter = Counter(value) return "".join(str(v) for v in chain.from_iterable(counter.items())) assert compress("aabbbccccddaa") == "a4b3c4d2"
-
a = list(input()) def k(c): b = 0 for i in range(len(a)-1): b += a.count(a[i]) for j in range(len(a), 0, -1):#길이?????? if a.count(a[i]) != 1: del a[i] return b == 1 print(a[i]+b,end="") print(k(a))
계속 고쳐보다가 지금은 여기서 멈추고 낑낑대는 중인데요 어떤걸 어떻게 고치면 좋을까요? ㅠㅠ
댓글 입력