#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param test_string string字符串 输入参数
# @return string字符串
#
class Solution:
def compressString(self , s ):
# write code here
#a空字符串
a = ''
#设立角标
c = 1
#字符长度
n = len(s)
#空字符判断
if n == 0:
return 'null'
#根据长度进行遍历
for i in range(n):
#第一个字符
if i==0:
a += s[0]
continue
#相等加一
if s[i]==s[i-1]:
c+=1
#结束时输出
if i==n-1:
a += str(c)
#不相等
else:
#输出角标大于1
if c>1:
a+=str(c)
#字母重置角标
else:
a+=str(c)
a+=s[i]
c=1
if i==n-1:
a+=str(c)
return a if n>len(a) else s




京公网安备 11010502036488号