을이 모든 당신이 문자열 (들)이 가장 긴 서브를 찾을 필요로하는 가정합니다 알파벳 순서로 문자열.
옵션 itertools 수입 C에서
test = s[0] # seed with first letter in string s
best = '' # empty var for keeping track of longest sequence
for n in range(1, len(s)): # have s[0] so compare to s[1]
if len(test) > len(best):
best = test
if s[n] >= s[n-1]:
test = test + s[n] # add s[1] to s[0] if greater or equal
else: # if not, do one of these options
test = s[n]
print "Longest substring in alphabetical order is:", best
옵션 B
maxSub, currentSub, previousChar = '', '', ''
for char in s:
if char >= previousChar:
currentSub = currentSub + char
if len(currentSub) > len(maxSub):
maxSub = currentSub
else: currentSub = char
previousChar = char
print maxSub
옵션 C
matches = []
current = [s[0]]
for index, character in enumerate(s[1:]):
if character >= s[index]: current.append(character)
else:
matches.append(current)
current = [character]
print "".join(max(matches, key=len))
옵션 D
def longest_ascending(s):
matches = []
current = [s[0]]
for index, character in enumerate(s[1:]):
if character >= s[index]:
current.append(character)
else:
matches.append(current)
current = [character]
matches.append(current)
return "".join(max(matches, key=len))
print(longest_ascending(s))
ountmaxsubstr = s [0 : 0] # 빈 슬래시 (str의 하위 클래스를 허용) 시작 범위 (len (s)) : # O (n) count (start + len (maxsubstr) + 1) : # O (m) len (set (substr))! = (end-start) : # 중복 또는 EOS가 발견 된 경우 중단되는 경우 substr = s [시작 : 종료] # O (m) – user2918562