参考 @ztxz16 的解答,改写了个简短的python
代码:
@ztxz16: https://blog.nowcoder.net/n/21d889d1febd4f9998e70280b813846a
def cal(s1, s2): dp = [[0] * 26 for _ in range(26)] # 在某个位置上,有dp[x1][x2]个s1[x1]和s2[x2] sums, res = 0, 0 for i in range(len(s1)): dp[ord(s1[i]) - ord('a')][ord(s2[i]) - ord('a')] += 1 sums += (s1[i] != s2[i]) return sums - max([dp[i][j] - dp[i][i] for j in range(26) for i in range(26)])