Longest substring in a string

Hello,

I have the following assignment:

’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ‘’ ’ ’ ‘’ ’ ’ ’

Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = ‘azcbobobegghakl’, then your program should print

Longest substring in alphabetical order is: beggh
In the case of ties, print the first substring. For example, if s = ‘abcbcd’, then your program should print

Longest substring in alphabetical order is: abc
’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ‘’ ’ ’ ‘’ ’ ’ ’

I have no idea on the outline of this algoroithm. Could somoeone please give me an idea?

Thank you.

english letters all have ASCII values. The ascii values are all contiguous in the lower case set of letters. (from 97 to 122)
If you can find a way to transform the letters to a list of ascii values then you can easily see sequences in the numbers.

Eg. if you had 97 98 99 (for a b c ) followed by 98 99 (b c)
then you can see that 97,98,99 is the longest sequence and translate that back to abc

Thanks a lot. I’ll try it.

1 Like