pythonedxlongest-substring

this code in python about finding the longest sub-string , i need explaination


i am trying to understand how this code works behind the scene , help appreciated

explain this part please:

MAX = ACCOUNT if len(ACCOUNT) > len(MAX) else MAX
ACCOUNT = ACCOUNT + c if c >= ACCOUNT[-1] else c

================================================

S = 'azcbobobegghaklbob'

ACCOUNT, MAX = S[0], ""

for c in S[1:] + " ":
    MAX = ACCOUNT if len(ACCOUNT) > len(MAX) else MAX
    ACCOUNT = ACCOUNT + c if c >= ACCOUNT[-1] else c

print('Longest substring in alphabetical order is:', MAX)

Solution

  • Writing out the code more clearly, it reads

    s = 'azcbobobegghaklbob'
    account = s[0]
    maximum = ''
    for c in s[1:] + ' ':
        if len(account) > len(maximum):
            maximum = account
        if c >= account[-1]:
            account += c
        else:
            account = c
    print('Longest substring in alphabetical order is:', maximum)
    

    maximum is the longest substring in alphabetical order so far. account is the current substring in alphabetical order that is being counted.

    The for loop iterates over 'zcbobobegghaklbob '. account starts out as the first character of the string. The first if block:

    if len(account) > len(maximum):
        maximum = account
    

    makes maximum equal to account if account has become larger than the previous maxumim.

    c is the next character to be analyzed, right after the last character of account. The second if block:

    if c >= account[-1]:
        account += c
    

    Sees if c comes after the last character of account, alphabetically. If it does, it adds it to the end of account. The >= comparison on strings sorts them lexicographical for alphabetical characters of the same case.

    The else cause:

    else:
        account = c
    

    clears the current substring and starts over if the next character is not in alphabetical order.

    In the end, maximum will be what you want.

    The extra ' ' is iterated over too so that the first if statement will be executed one more time before the loop finishes. In python, ' ' comes before all alphabetical characters, lexicographically.