Find the longest substring in alphabetical order

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

    In Python character comparison is easy compared to java script where the ASCII values have to be compared. According to python

    a>b gives a Boolean False and b>a gives a Boolean True

    Using this the longest sub string in alphabetical order can be found by using the following algorithm :

    def comp(a,b):
        if a<=b:
            return True
        else:
            return False
    s = raw_input("Enter the required sting: ")
    final = []
    nIndex = 0
    temp = []
    for i in range(nIndex, len(s)-1):
        res = comp(s[i], s[i+1])
        if res == True:       
               if temp == []:
               #print i
                   temp.append(s[i])
                   temp.append(s[i+1])
               else:
                   temp.append(s[i+1])
           final.append(temp)
            else:
           if temp == []:
            #print i
            temp.append(s[i])
           final.append(temp)
           temp = []
    lengths = []
    for el in final:
        lengths.append(len(el))
    print lengths
    print final
    lngStr = ''.join(final[lengths.index(max(lengths))])
    print "Longest substring in alphabetical order is: " + lngStr
    
    0 讨论(0)
  • 2020-11-30 10:00
    s=input()
    temp=s[0]
    output=s[0]
    for i in range(len(s)-1):
        if s[i]<=s[i+1]:
            temp=temp+s[i+1]
            if len(temp)>len(output):
                output=temp           
        else:
            temp=s[i+1]
    
    print('Longest substring in alphabetic order is:' + output)
    
    0 讨论(0)
  • 2020-11-30 10:01

    I had similar question on one of the tests on EDX online something. Spent 20 minutes brainstorming and couldn't find solution. But the answer got to me. And it is very simple. The thing that stopped me on other solutions - the cursor should not stop or have unique value so to say if we have the edx string s = 'azcbobobegghakl' it should output - 'beggh' not 'begh'(unique set) or 'kl'(as per the longest identical to alphabet string). Here is my answer and it works

    n=0
    for i in range(1,len(s)):
        if s[n:i]==''.join(sorted(s[n:i])):
            longest_try=s[n:i]
        else:
            n+=1
    
    0 讨论(0)
  • 2020-11-30 10:02

    You can improve your algorithm by noticing that the string can be broken into runs of ordered substrings of maximal length. Any ordered substring must be contained in one of these runs

    This allows you to just iterate once through the string O(n)

    def longest_substring(string):
        curr, subs = '', ''
        for char in string:
            if not curr or char >= curr[-1]:
                curr += char
            else:
                curr, subs = '', max(curr, subs, key=len)
        return max(curr, subs, key=len)
    
    0 讨论(0)
  • 2020-11-30 10:04

    This worked for me

    s = 'cyqfjhcclkbxpbojgkar'
    
    lstring = s[0]
    slen = 1
    
    for i in range(len(s)):
        for j in range(i,len(s)-1):
                if s[j+1] >= s[j]:
                        if (j+1)-i+1 > slen:
                            lstring = s[i:(j+1)+1]
                            slen = (j+1)-i+1
                else:
                            break
    
    print("Longest substring in alphabetical order is: " + lstring)
    

    Output: Longest substring in alphabetical order is: ccl

    0 讨论(0)
提交回复
热议问题