题解 | 小红的字符串修改

小红的字符串修改

https://www.nowcoder.com/practice/66e0054ff6b345afa47bcd4e8ceb72d7

import sys

def char_cost(c1,c2):
    diff = abs(ord(c1) - ord(c2))
    return min(diff,26-diff)


s = input().strip()
t = input().strip()
res = 99999

lens = len(s)
lent = len(t)
# 枚举所有字串
for i in range(lent-lens+1):
    t_sub = t[i:i+lens]
    cost = 0
    for j in range(lens):
        cost += char_cost(t_sub[j],s[j])
    res = min(res,cost)

print(res)

枚举t中所有长度=lens的子串

全部评论

相关推荐

12-27 22:14
门头沟学院 Java
点赞 评论 收藏
分享
11-03 13:18
门头沟学院 Java
包行:平时怎么刷算法题的哇,字节的手撕听说都很难
字节跳动工作体验
点赞 评论 收藏
分享
迷茫的大四🐶:能不能好好排个版,谁会看这么长的简历啊,说明书吗
校招求职吐槽
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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