题解 | 小红的字符串修改

小红的字符串修改

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的子串

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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