Finding longest substring in alphabetical order

后端 未结 17 1429
刺人心
刺人心 2020-12-06 15:15

EDIT: I am aware that a question with similar task was already asked in SO but I\'m interested to find out the problem in this specific piece of code. I am

17条回答
  •  [愿得一人]
    2020-12-06 16:04

    s = 'gkuencgybsbezzilbfg'
    x = s.lower()
    y = ''
    z = [] #creating an empty listing which will get filled
    
    for i in range(0,len(x)):
        if i == len(x)-1:
           y = y + str(x[i])
           z.append(y)
           break 
        a = x[i] <= x[i+1]
        if a == True:
            y = y + str(x[i])
        else: 
            y = y + str(x[i])
            z.append(y)  # fill the list 
            y = ''
    # search of 1st longest string        
    L = len(max(z,key=len))      # key=len takes length in consideration 
    for i in range(0,len(z)):
        a = len(z[i])
        if a == L:   
            print 'Longest substring in alphabetical order is:' + str(z[i])
            break
    

提交回复
热议问题