Skip to content

Instantly share code, notes, and snippets.

@sunilk-n
Created October 22, 2017 18:08
Show Gist options
  • Save sunilk-n/79db54a863746e232e008a3ca0e46235 to your computer and use it in GitHub Desktop.
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"
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