题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

ent = input()
ent_list = list(ent)
dic = {}
for i, s in enumerate(ent):
    if ent_list[i].isalpha():
        for j in range(i+1, len(ent)):
            if ent_list[j].isalpha():
                if ord(ent_list[i].lower()) > ord(ent_list[j].lower()):
                    ent_list[i], ent_list[j] = ent_list[j], ent_list[i]
    if s.isalpha():
        if s.lower() not in dic:
            dic[s.lower()] = [s]
        else:
            dic[s.lower()].append(s)
vis = {k: 0 for k in dic.keys()}
for i in range(len(ent_list)):
    if ent_list[i].isalpha():
        ent_list[i] = dic[ent_list[i].lower()][vis[ent_list[i].lower()]]
        vis[ent_list[i].lower()] += 1
print(*ent_list, sep='')

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务