題目描述
輸入一串字符,請編寫一個字符串壓縮程序,將字符串中連續出現的重復字母進行壓縮,並輸出壓縮后的字符串。
例如:
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)