問題
B – 高橋くんと文字列圧縮
回答
s = input() s_compressed = '' cnt = 1 for i, ch in enumerate(s): if i == 0: ch_pre = ch continue if ch == s[i-1]: cnt += 1 else: s_compressed += ch_pre + str(cnt) ch_pre = ch cnt = 1 s_compressed += ch_pre + str(cnt) print(s_compressed)