Q:
Assume s is a string of lower case characters.
Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print
Longest substring in alphabetical order is: beggh
A:
curString = s[0]
longest = s[0]
for i in range(1, len(s)):
if s[i] >= curString[-1]:
curString += s[i]
if len(curString) > len(longest):
longest = curString
else:
curString = s[i]
print 'Longest substring in alphabetical order is:', longest思路整理:初始化: 连续的字符串为s[0]; 最长的连续字符串是s[0]如果s中的字符大于或等于连续字符串中的最后一个字符,则累加,并改变最长字符串的值;否则重新设置连续字符串的首字符为s[i]
转载请注明原文地址: https://ju.6miu.com/read-670646.html