Find the longest substring in alphabetical order

前端 未结 17 2366
有刺的猬
有刺的猬 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:47
    s = 'cyqfjhcclkbxpbojgkar'
    longest = ""
    max =""
    
    for i in range(len(s) -1):
        if(s[i] <= s[i+1] ):
           longest = longest + s[i]
           if(i==len(s) -2):
               longest = longest + s[i+1]
        else:
            longest  = longest + s[i]        
            if(len(longest) > len(max)):
                max = longest
            longest = ""        
    
    if(len(s) == 1):
        longest = s
    
    
    if(len(longest) > len(max)):
        print("Longest substring in alphabetical order is: " + longest)
    else:
        print("Longest substring in alphabetical order is: " + max)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-30 09:51

    finding the longest substring in alphabetical order in Python

    in python shell 'a' < 'b' or 'a' <= 'a' is True

    result = ''
    temp = ''
    for char in s:
        if (not temp or temp[-1] <= char):
            temp += char
        elif (temp[-1] > char):
            if (len(result) < len(temp)):
                result = temp
            temp = char
        
    if (len(temp) > len(result)):
        result = temp
        
    print('Longest substring in alphabetical order is:', result)
    
    0 讨论(0)
  • 2020-11-30 09:55

    Another way:

    s = input("Please enter a sentence: ")
    count = 0
    maxcount = 0
    result = 0
    for char in range(len(s)-1):
        if(s[char]<=s[char+1]):
           count += 1        
        if(count > maxcount):
               maxcount = count
               result = char + 1
    
        else:
    
            count = 0
    startposition = result - maxcount
    print("Longest substring in alphabetical order is: ", s[startposition:result+1])
    
    0 讨论(0)
  • 2020-11-30 09:56

    Slightly different implementation, building up a list of all substrings in alphabetical order and returning the longest one:

    def longest_substring(s):
        in_orders = ['' for i in range(len(s))]
        index = 0
        for i in range(len(s)):
            if (i == len(s) - 1 and s[i] >= s[i - 1]) or s[i] <= s[i + 1]:
                in_orders[index] += s[i]
            else:
                in_orders[index] += s[i]
                index += 1
        return max(in_orders, key=len)  
    
    0 讨论(0)
  • 2020-11-30 09:56
    input_str = "cyqfjhcclkbxpbojgkar"
    length = len(input_str) # length of the input string
    iter = 0
    result_str = '' # contains latest processed sub string
    longest = '' # contains longest sub string alphabetic order 
    while length > 1: # loop till all char processed from string
        count = 1
        key = input_str[iter] #set last checked char as key 
        result_str += key # start of the new sub string
        for i in range(iter+1, len(input_str)): # discard processed char to set new range
          length -= 1
          if(key <= input_str[i]): # check the char is in alphabetic order 
            key = input_str[i]
            result_str += key # concatenate the char to result_str
            count += 1
          else:
            if(len(longest) < len(result_str)): # check result and longest str length
              longest = result_str # if yes set longest to result
            result_str = '' # re initiate result_str for new sub string
            iter += count # update iter value to point the index of last processed char
            break
    
        if length is 1: # check for the last iteration of while loop
            if(len(longest) < len(result_str)):
              longest = result_str
    
    print(longest);
    
    0 讨论(0)
提交回复
热议问题