题解 | 小红的字符串修改
小红的字符串修改
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的子串


正浩创新EcoFlow公司福利 647人发布