具体题目为:
将连续超过四位的字母进行简写,其他的则直接保留:
比如
abcdfqwer ->a-fqwer
abc -> abc
abcd -> a-d
def encoderStr(s):
n = len(s)
low,high = 0,1
newStr = ''
s+='*' #此处为了将所有字符都进行遍历
while(high<n):
for i in range(low+1,n+1):
if(ord(s[i])-ord(s[i-1]) == 1):
high = i
else:
high += 1 #保证无论如何都要向前走一步,避免出现while无限循环
break
if(high-low >=3):
newStr += s[low]+'-'+s[high-1]
else:
newStr += s[low:high]
low = high
return newStr
input: encoderStr('abcdefgabcdasdfcdefopqrst')
output: 'a-ga-dasdfc-fo-t'