Find the longest substring in alphabetical order

前端 未结 17 2432
有刺的猬
有刺的猬 2020-11-30 09:12

I have this code that I found on another topic, but it sorts the substring by contiguous characters and not by alphabetical order. How do I correct it for alphabetical order

17条回答
  •  [愿得一人]
    2020-11-30 09:48

    s = "azcbobobegghakl"
    ls = ""
    for i in range(0, len(s)-1):
        b = ""
        ss = ""
        j = 2
        while j < len(s):
            ss = s[i:i+j]
            b = sorted(ss)
            str1 = ''.join(b)
            j += 1
            if str1 == ss:
                ks = ss
            else:
                break
        if len(ks) > len(ls):
            ls = ks
    print("The Longest substring in alphabetical order is "+ls)
    

提交回复
热议问题