Created
October 22, 2017 18:08
-
-
Save sunilk-n/79db54a863746e232e008a3ca0e46235 to your computer and use it in GitHub Desktop.
Write a program that prints the longest sub-string of a string in which the letters occur i alphabetical order. For example, s="azcbobobegghakl" then your program should print "Longest string in alphabetical order is: beggh"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = "abcbcd" | |
alph = "abcdefghijklmnopqrstuvwxyz" | |
temp = 0 | |
list = "" | |
fin = [] | |
for i in range(len(s)): | |
if s[i] in alph: | |
fin.append(list) | |
list = list + s[i] | |
prev = alph.index(s[i]) | |
if prev >= temp: | |
temp = prev | |
else: | |
temp = 0 | |
list = "" | |
prev = alph.index(s[i]) | |
if prev >= temp: | |
temp = prev | |
list = list + s[i] | |
fin.append(list) | |
#print fin | |
max = 0 | |
get = "" | |
for each in fin: | |
if max<len(each): | |
max = len(each) | |
get = each | |
print "Longest string in alphabetical order is: "+get |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment