Find the longest substring in alphabetical order

前端 未结 17 2358
有刺的猬
有刺的猬 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:37

    Try changing this:

            if len(set(substr)) != (end - start): # found duplicates or EOS
                break
            if (ord(max(sorted(substr))) - ord(min(sorted(substr))) + 1) == len(substr):
    

    to this:

            if len(substr) != (end - start): # found duplicates or EOS
                break
            if sorted(substr) == list(substr):
    

    That will display ccl for your example input string. The code is simpler because you're trying to solve a simpler problem :-)

提交回复
热议问题