Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the longest substring in alphabetical order

Tags:

python

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? It prints out lk, and I want to print ccl. Thanks

ps: I'm a beginner in python

s = 'cyqfjhcclkbxpbojgkar'
from itertools import count

def long_alphabet(input_string):
    maxsubstr = input_string[0:0] # empty slice (to accept subclasses of str)
    for start in range(len(input_string)): # O(n)
        for end in count(start + len(maxsubstr) + 1): # O(m)
            substr = input_string[start:end] # O(m)
            if len(set(substr)) != (end - start): # found duplicates or EOS
                break
            if (ord(max(sorted(substr))) - ord(min(sorted(substr))) + 1) == len(substr):
                maxsubstr = substr
    return maxsubstr

bla = (long_alphabet(s))
print "Longest substring in alphabetical order is: %s" %bla
like image 497
spacegame Avatar asked Oct 26 '13 01:10

spacegame


People also ask

How do you find a string in alphabetical order?

A simple approach: Store the string to a character array and sort the array. If the characters in the sorted array are in the same order as the string then print 'In alphabetical order '. Print 'Not in alphabetical order' otherwise.

How do you find the longest repeating substring?

Longest Repeating Substring in C++ Suppose we have a string S, we have to find the length of the longest repeating substring(s). We will return 0 if no repeating substring is present. So if the string is like “abbaba”, then the output will be 2. As the longest repeating substring is “ab” or “ba”.

How do you find the longest substring in Python?

Use a While Loop to Get the Longest Substring in Python We will go through the while loop until we have found the longest substring from the given string. As you can see from the above example, the longest substring possible has a length of 12, the same as the ABCGHIJKLMNO substring from the original string.


2 Answers

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)
like image 152
ejrb Avatar answered Nov 13 '22 08:11

ejrb


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)
like image 26
user3337142 Avatar answered Nov 13 '22 10:11

user3337142