题解 | 字符串排序

import sys

def sort_alpha(s_lst) -> str:
    sorted_s = sorted(s_lst, key=lambda x: (ord(x[0]) - ord('A') if x[0].isupper() else ord(x[0])  - ord('a'), x[1]))
    return ''.join([x[0] for x in sorted_s])

raw_input = []
for i,line in enumerate(sys.stdin):
    raw_input.append(line.strip())
    if i == 1:
        break

input_str = raw_input[0]
res = ''
idx = 0
alpha_lst = [c for c in input_str if c.isalpha()]
alpha_idx_lst = [(c, i) for i,c in enumerate(alpha_lst)]
sorted_alphas = sort_alpha(alpha_idx_lst)
# print(sorted_alphas)
for c in input_str:
    if c.isalpha():
        res += sorted_alphas[idx]
        idx += 1
    else:
        res += c

print(res)

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 13:47
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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