s = 'abcabcabc'
i = 0
j = 1
longestSubString = ''
realString = ''
while (j < len(s)):
if i == 0:
longestSubString = s[i]
if (s[j] >= s[i]):
longestSubString = longestSubString + s[i]
if len(longestSubString) > len (realString):
realString = longestSubString
i += 1
j += 1
else:
longestSubString = ''
i += 1
j += 1
print ("Longest SubString is: " + realString)
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 beggh
After spending hours in building the code I didn't get the desired result. Can someone please have a look at my code and guide me wherever I am wrong.