Alphabetical Substrings

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, ifs = '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

s='abcbcd'

count=1
result=s[0]
while s:
    newcount=1
    newresult=''
    i=0
    while i+1<len(s):
        if ord(s[i])<=ord(s[i+1]):
            newresult+=s[i+1]
            newcount+=1
        else:
            break
        i+=1
    if newcount>count:
        count=newcount
        result=s[0]+newresult
    s=s[i+1:]
print result





全部评论

相关推荐

昨天 13:12
已编辑
门头沟学院 Java
斯卡蒂味的鱼汤:知道你不会来数马,就不捞你😂最近数马疯狂扩招,招聘要求挺低的,你能力肯定够,应该就是因为太强了,知道你不会来才不捞你
投递腾讯云智研发等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务