Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python 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.
 
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.
Hi there,
Have you given it a shot and lost your way? If so, feel free to post what you have already. :) Otherwise, from the sound of the prompt, you will need some sort of looping to go through each individual characters, and checking them against some sort of way to tell if each character proceeds the current one alphabetically ;)
 
Loop through the string character-by-character and decide what you need to do in each of 4 cases:
  1. The character is the first in the string
  2. The character is not the first and is lower in the alphabet than the preceding character
  3. The character is not the first and is equal to the preceding character
  4. The character is not the first and is higher in the alphabet than the preceding character
Also think about what is needed when the loop has finished.

Depending on your programming experience, it may be helpful to first describe that process in pseudo-code, rather than starting programming right away and get bogged down in syntax details.
 
I find it helpful to work through an example by hand. Write your string on a piece of paper. Make two columns and call one sub (for substring) and the other one longest (for longest substring). Start by writing the first letter under sub. As you step through the letters, if the next letter is >= the previous letter, add it to the end of sub. If it is not then decide if the current sub string is longer than the current longest. If it is then you have found a new longest and write that string under the longest column. Then reset sub to the current letter.

Once you have gone through all the letters you will have two strings, sub and longest. Whichever one has the most letters is the string you want.

Try coding that up with the following as a starting point:
Python:
def longest(s: str) -> str:
    .
    .
    .
 
Back
Top Bottom