按字母顺序查找最长的子字符串。我的代码有什么问题

Find the longest substring in alphabetical order. What is wrong with my code

到目前为止我得到了:

s = 'azcbobobegghakl'
i = 0
j = 1 

temp = ''  #temporary variable I use to change longest
longest = ''   
for b in range(len(s)):
    if s[i] <= s[j]: #checks if it's in alphabetical order
        temp+=s[i] 
        if len(longest) < len(temp):
           longest  = temp       
    else: 
        temp = ''    #reset temp after every substring  
    if len(longest) < len(temp):
        longest  += temp      #update longest if the new substring is longer
    i +=1
    if j < len(s) - 1 : # because otherwise i get an IndexError
       j +=1


print('Longest substring in alphabetical order is:', longest)

现在我的问题是我总是得到正确的字符串但没有最后一个字母。 (例如这里我得到 "begg" 而不是 "beggh")。

我也必须这样做,如果平局,第一个算最长的(如果s = abcbcd,abc应该是最长的)

最后我知道这个问题已经在这里被问过好几次了,但我想尽可能修复我想出的代码,而不是完全使用别人的代码。

您正在比较 s[i]s[i+1],但只是将 s[i] 添加到 temp。而是始终以 temp 中的当前字母开始,并在比较

之后将 s[i+1] 添加到 temp
s = 'azcbobobegghakl'
i = 0

temp = s[0]   #temporary variable I use to change longest
longest = ''   
for i in range(len(s)-1):
    if s[i] <= s[i+1]:  #checks if it's in alphabetical order
        temp+=s[i+1]
        if len(longest) < len(temp):
           longest  = temp       
    else: 
        temp = s[i+1]     #reset temp after every substring
    if len(longest) < len(temp):
        longest  += temp    #update longest if the new substring is longer
    i +=1



print('Longest substring in alphabetical order is:', longest)

# Longest substring in alphabetical order is: beggh

如果您发现下一个字符不按字母顺序排列,则说明您没有考虑当前字符,这就是为什么您最后漏掉了一个字符。

相反,如果下一个字符不按顺序,您应该始终先处理当前字符,然后再重置 temp

s = 'azcbobobegghakl'
i = 0

temp = ''   #temporary variable I use to change longest
longest = ''   
for i in range(len(s)-1):
    temp += s[i]
    if len(longest) < len(temp):
        longest = temp
    if s[i] > s[i+1]:  #checks if it's in alphabetical order
        temp = ''     #reset temp after every substring
    if len(longest) < len(temp):
        longest  += temp    #update longest if the new substring is longer  
    i +=1



print('Longest substring in alphabetical order is:', longest)

这输出:

Longest substring in alphabetical order is: beggh
s = 'azcbobobegghakl'
i = 0
j = 1 

temp = ''  #temporary variable I use to change longest
longest = '' 


for b in range(0, len(s) -1):
    if s[b] <= s[b + 1]: #checks if it's in alphabetical order
        if len(temp) == 0:
            temp+= s[b]
            temp+= s[b+1]
        else:
            temp+= s[b+1]

        if len(longest) < len(temp):
           longest  = temp       
    else: 
        temp = ''    #reset temp after every substring  
    if len(longest) < len(temp):
        longest  += temp      #update longest if the new substring is longer
    i +=1
    if j < len(s) - 1 : # because otherwise i get an IndexError
       j +=1


print('Longest substring in alphabetical order is:', longest)

虽然我在其他答案中提到了如何修复您的代码,但我还想说直接的方法是使用 itertools.accumulate

list(accumulate(s, lambda s, c: s+c if s[-1]<=c else c) )
# ['a', 'az', 'c', 'b', 'bo', 'b', 'bo', 'b', 'be', 'beg', 'begg', 'beggh', 'a', 'ak', 'akl']

所以按字母顺序查找最长子串的一行代码是

sorted(accumulate(s, lambda s, c: s+c if s[-1]<=c else c), key=len)[-1]
# 'beggh'

要在最后解决问题,您需要在代码到达最终索引时对其进行某种中断。此外,如果您到达字符串的末尾并且它仍然按字母顺序排列(见下文),您需要添加一些代码来重置循环。最后,对于最长字符串只有一个字符的情况,你需要为longest_word和s[0}处的单词定义你的起始变量。坚持下去,你最终会得到它。 pythontutor.com 是调试和故障排除的好工具。

s = 'abcdefghijklmnopqrstuvwxyz'
a = 0
b = 1
longest_word = s[0]
word = s[0]
for i in range(len(s)):
    a = 0
    b = 1
    if i == len(s) - 2 or b > len(s)-1:
        i += 1
        print('Longest substring in alphabetical order is:', longest_word)
        break
    while (b < len(s)-i) and (s[i+a] <= s[i+b]):
        word = s[i:i+b+1]
        a += 1
        b += 1
    if len(word) > len(longest_word):
        longest_word = s[i:i+b]
    else: 
        i += 1