我正在学校学习 Python 2.7 课程,他们告诉我们创建以下程序:
假设 s 是一串小写字符。
编写一个程序,打印 s 中字母按字母顺序出现的最长子串。
例如,如果 s = azcbobobegghakl ,那么您的程序应该打印
按字母顺序排列的最长子串是:beggh
如果是平局,则打印第一个子字符串。
例如,如果 s = 'abbcbcd',那么你的程序应该打印
按字母顺序排列的最长子串是:abc
我写了以下代码:
s = 'czqriqfsqteavw'
string = ''
tempIndex = 0
prev = ''
curr = ''
index = 0
while index < len(s):
curr = s[index]
if index != 0:
if curr < prev:
if len(s[tempIndex:index]) > len(string):
string = s[tempIndex:index]
tempIndex=index
elif index == len(s)-1:
if len(s[tempIndex:index]) > len(string):
string = s[tempIndex:index+1]
prev = curr
index += …Run Code Online (Sandbox Code Playgroud)