题目描述:
统计字符串中连续字符的个数,若间隔则重新统计。
输入描述:
abbacdd
输出描述:
a_1_b_2_a_1_c_1_d_2
代码:
def count(s):
i,j=0,0
mylist=[]
while(i<len(s)):
while(j<len(s) and s[i]==s[j]):
j+=1
if j==len(s):
mylist.append(s[i]+"_"+str(j-i))
break
if s[i]!=s[j]:
mylist.append(s[i]+"_"+str(j-i))
i = j
print("_".join(mylist))
s="abbacdd"
count(s)