Finding longest substring in alphabetical order

后端 未结 17 1430
刺人心
刺人心 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 15:40

    a lot more looping, but it gets the job done

    s = raw_input("Enter string")
    fin=""
    s_pos =0
    while s_pos < len(s):
        n=1
        lng=" "
        for c in s[s_pos:]:
            if c >= lng[n-1]:
                lng+=c
                n+=1
            else :
                break
        if len(lng) > len(fin):
            fin= lng`enter code here`
        s_pos+=1    
    print "Longest string: " + fin
    
    0 讨论(0)
  • 2020-12-06 15:40
    s = 'azcbobobegghakl' 
    
    i=1
    subs=s[0]
    subs2=s[0]
    while(i<len(s)):
        j=i
        while(j<len(s)):
            if(s[j]>=s[j-1]):
                subs+=s[j]
                j+=1 
            else:
                subs=subs.replace(subs[:len(subs)],s[i])   
                break
    
            if(len(subs)>len(subs2)):
                subs2=subs2.replace(subs2[:len(subs2)], subs[:len(subs)])
        subs=subs.replace(subs[:len(subs)],s[i])
        i+=1
    print("Longest substring in alphabetical order is:",subs2)
    
    0 讨论(0)
  • 2020-12-06 15:42
    s = input("insert some string: ")
    start = 0
    end = 0
    temp = ""
    while end+1 <len(s):
        while end+1 <len(s) and s[end+1] >= s[end]:
            end += 1
        if len(s[start:end+1]) > len(temp):
            temp = s[start:end+1]
        end +=1
        start = end
    print("longest ordered part is: "+temp)
    
    0 讨论(0)
  • 2020-12-06 15:44

    There are many things to improve in your code but making minimum changes so as to make it work. The problem is you should have if last_pos(i) != None: in your for loop (i instead of i+1) and you should compare diff (not diff - 1) against maxLen. Please read other answers to learn how to do it better.

    for i in range(len(s)):
        if last_pos(i) != None:
            diff = last_pos(i) - i + 1
        if diff > maxLen:
            maxLen = diff
            startPos = i
            endPos = startPos + diff - 1
    
    0 讨论(0)
  • 2020-12-06 15:45

    Simple and easy.

    Code :

    s = 'hixwluvyhzzzdgd' 
    r,p,t = '','',''
    for c in s:
        if p <= c:
            t += c
            p = c
        else:
            if len(t) > len(r):
                r = t
            t,p = c,c
    if len(t) > len(r):
        r = t
    print 'Longest substring in alphabetical order is: ' + r
    

    Output :

    Longest substring in alphabetical order which appeared first: luvy
    
    0 讨论(0)
  • 2020-12-06 15:45

    Here is a single pass solution with a fast loop. It reads each character only once. Inside the loop operations are limited to

    • 1 string comparison (1 char x 1 char)
    • 1 integer increment
    • 2 integer subtractions
    • 1 integer comparison
    • 1 to 3 integer assignments
    • 1 string assignment

    No containers are used. No function calls are made. The empty string is handled without special-case code. All character codes, including chr(0), are properly handled. If there is a tie for the longest alphabetical substring, the function returns the first winning substring it encountered. Case is ignored for purposes of alphabetization, but case is preserved in the output substring.

    def longest_alphabetical_substring(string):
        start, end = 0, 0     # range of current alphabetical string
        START, END = 0, 0     # range of longest alphabetical string yet found
        prev = chr(0)         # previous character
    
        for char in string.lower():   # scan string ignoring case
            if char < prev:       # is character out of alphabetical order?
                start = end       #     if so, start a new substring 
            end += 1              # either way, increment substring length 
    
            if end - start > END - START:  # found new longest?  
                START, END = start, end    #     if so, update longest 
            prev = char                    # remember previous character
    
        return string[START : END]   # return longest alphabetical substring 
    

    Result

    >>> longest_alphabetical_substring('drurotsxjehlwfwgygygxz')
    'ehlw'
    >>> longest_alphabetical_substring('eseoojlsuai')
    'jlsu'
    >>> longest_alphabetical_substring('hixwluvyhzzzdgd')
    'luvy'
    >>>
    
    0 讨论(0)
提交回复
热议问题