-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProb3_Solution.py
25 lines (19 loc) · 1.19 KB
/
Prob3_Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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
# Note: This problem may be challenging. We encourage you to work smart.
# If you've spent more than a few hours on this problem, we suggest that you move on to a different part of the course.
# If you have time, come back to this problem after you've had a break and cleared your head.
current = ''
longest = ''
for i in range(len(s)):
if (s[i] >= s[i-1]): # it is like this because in "abc" c > b > a it should line by its value
current += s[i] # if letter bigger than the one before, it's okay. else: keep [i]
else:
current = s[i]
if len(current) > len(longest): # if current bigger than 0 :D just put it in longest.
longest = current
print("Longest substring in alphabetical order is: " + longest)