在python中查找连续字母的最长子字符串

在python中查找连续字母的最长子字符串,python,regex,python-3.x,Python,Regex,Python 3.x,我想使用python查找连续字母的最长子字符串 企图 我如何解决这个问题 from itertools import count def f(input_string): maxsubstr = input_string[0:0] for start in range(len(input_string)): for end in count(start + len(maxsubstr) + 1): substr = input_strin

我想使用python查找连续字母的最长子字符串

企图 我如何解决这个问题

from itertools import count

def f(input_string):
    maxsubstr = input_string[0:0]
    for start in range(len(input_string)):
        for end in count(start + len(maxsubstr) + 1):
            substr = input_string[start:end]
            if len(set(substr)) != (end - start):
                break
            if (ord(max(substr)) - ord(min(substr)) + 1) == len(substr):
                maxsubstr = substr
    print ('The longest substring of consecutive letters has a length of {}.'.format(len(maxsubstr)))
    print ('The leftmost such substring is {}.'.format(maxsubstr))

f('x')
f('xy')
f('ababcuvwaba')
f('abbcedffghiefghiaaabbcdefgg')
f('abcabccdefcdefghacdef')
输出:

The longest substring of consecutive letters has a length of 1.
The leftmost such substring is x.
The longest substring of consecutive letters has a length of 2.
The leftmost such substring is xy.
The longest substring of consecutive letters has a length of 3.
The leftmost such substring is abc.
The longest substring of consecutive letters has a length of 6.
The leftmost such substring is bcdefg.
The longest substring of consecutive letters has a length of 6.
The leftmost such substring is cdefgh.

将标题和说明更改为按字母顺序排列的最长子字符串。。我知道你在那里做了什么。
from itertools import count

def f(input_string):
    maxsubstr = input_string[0:0]
    for start in range(len(input_string)):
        for end in count(start + len(maxsubstr) + 1):
            substr = input_string[start:end]
            if len(set(substr)) != (end - start):
                break
            if (ord(max(substr)) - ord(min(substr)) + 1) == len(substr):
                maxsubstr = substr
    print ('The longest substring of consecutive letters has a length of {}.'.format(len(maxsubstr)))
    print ('The leftmost such substring is {}.'.format(maxsubstr))

f('x')
f('xy')
f('ababcuvwaba')
f('abbcedffghiefghiaaabbcdefgg')
f('abcabccdefcdefghacdef')
The longest substring of consecutive letters has a length of 1.
The leftmost such substring is x.
The longest substring of consecutive letters has a length of 2.
The leftmost such substring is xy.
The longest substring of consecutive letters has a length of 3.
The leftmost such substring is abc.
The longest substring of consecutive letters has a length of 6.
The leftmost such substring is bcdefg.
The longest substring of consecutive letters has a length of 6.
The leftmost such substring is cdefgh.