Find the longest substring in alphabetical order

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

    Try changing this:

            if len(set(substr)) != (end - start): # found duplicates or EOS
                break
            if (ord(max(sorted(substr))) - ord(min(sorted(substr))) + 1) == len(substr):
    

    to this:

            if len(substr) != (end - start): # found duplicates or EOS
                break
            if sorted(substr) == list(substr):
    

    That will display ccl for your example input string. The code is simpler because you're trying to solve a simpler problem :-)

    0 讨论(0)
  • 2020-11-30 09:37

    Wow, some really impressing code snippets here... I want to add my solution, as I think it's quite clean:

    s = 'cyqfjhcclkbxpbojgkar'
    
    res = ''
    tmp = ''
    
    for i in range(len(s)):
        tmp += s[i]
        if len(tmp) > len(res):
            res = tmp
        if i > len(s)-2:
            break
        if s[i] > s[i+1]:
            tmp = ''
    
    print("Longest substring in alphabetical order is: {}".format(res))
    
    0 讨论(0)
  • 2020-11-30 09:40

    Use list and max function to reduce the code drastically.

    actual_string = 'azcbobobegghakl'
    strlist = []
    i = 0
    while i < len(actual_string)-1:
        substr = ''
        while actial_string[i + 1] > actual_string[i] :
            substr += actual_string[i]
            i += 1
            if i > len(actual_string)-2:
                break
        substr += actual-string[i]
        i += 1
        strlist.append(subst)
    print(max(strlist, key=len))
    
    0 讨论(0)
  • 2020-11-30 09:42

    In a recursive way, you can import count from itertools

    Or define a same method:

    def loops( I=0, S=1 ):
        n = I
        while True:
            yield n
            n += S
    

    With this method, you can obtain the value of an endpoint, when you create any substring in your anallitic process.

    Now looks the anallize method (based on spacegame issue and Mr. Tim Petters suggestion)

    def anallize(inStr):
        # empty slice (maxStr) to implement
        # str native methods
        # in the anallize search execution
        maxStr = inStr[0:0]
        # loop to read the input string (inStr)
        for i in range(len(inStr)):
            # loop to sort and compare each new substring
            # the loop uses the loops method of past
            # I = sum of: 
            #     (i) current read index
            #     (len(maxStr)) current answer length
            #     and 1
            for o in loops(i + len(maxStr) + 1):
                # create a new substring (newStr)
                # the substring is taked:
                # from: index of read loop (i)
                # to:   index of sort and compare loop (o)
                newStr = inStr[i:o]
    
                if len(newStr) != (o - i):# detect and found duplicates
                    break
                if sorted(newStr) == list(newStr):# compares if sorted string is equal to listed string
                    # if success, the substring of sort and compare is assigned as answer
                    maxStr = newStr
        # return the string recovered as longest substring
        return maxStr
    

    Finally, for test or execution pourposes:

    # for execution pourposes of the exercise:
    s = "azcbobobegghakl"
    print "Longest substring in alphabetical order is: " + anallize( s )
    

    The great piece of this job started by: spacegame and attended by Mr. Tim Petters, is in the use of the native str methods and the reusability of the code.

    The answer is:

    Longest substring in alphabetical order is: ccl

    0 讨论(0)
  • 2020-11-30 09:44
    s = 'cyqfjhcclkbxpbojgkar'
    r = ''
    c = ''
    for char in s:
        if (c == ''):
            c = char
        elif (c[-1] <= char):
            c += char
        elif (c[-1] > char):
            if (len(r) < len(c)):
                r = c
                c = char
            else:
                c = char
    if (len(c) > len(r)):
        r = c
    print(r)
    
    0 讨论(0)
  • 2020-11-30 09:44

    Without using a library, but using a function ord() which returns ascii value for a character. Assumption: input will be in lowercase, and no special characters are used

    s = 'azcbobobegghakl'
    
    longest = ''
    
    for i in range(len(s)):
        temp_longest=s[i]
    
        for j in range(i+1,len(s)):
    
            if ord(s[i])<=ord(s[j]):
                temp_longest+=s[j]
                i+=1
            else:
                break
    
        if len(temp_longest)>len(longest):
            longest = temp_longest
    
    print(longest)
    
    0 讨论(0)
提交回复
热议问题