题目描述
输入一串字符,请编写一个字符串压缩程序,将字符串中连续出现的重复字母进行压缩,并输出压缩后的字符串。
例如:
aac 压缩为 1ac
xxxxyyyyyyzbbb 压缩为 3x5yz2b
输入描述:
任意长度字符串
输出描述:
压缩后的字符串
示例1
输入
xxxxyyyyyyzbbb
输出
3x5yz2b
参考:
s = input() i = 0 count = 0 res = '' while i < len(s): count = 0 while i+1 < len(s) and s[i] == s[i+1]: count += 1 i += 1 if count != 0: res += str(count) res += s[i] i += 1 print(res)