题解 | 字符串距离计算
字符串距离计算
https://www.nowcoder.com/practice/82bd533cd9c34df29ba15bbf1591bedf
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 计算最少的距离
# @param S1 string字符串 第一个字符串
# @param S2 string字符串 第二个字符串
# @return int整型
#
class Solution:
def GetMinDistance(self , s1 , s2 ):
# write code here
list_num=[]
if len(s1)!=len(s2):
return None
if len(s1)>5e4 or len(s1)<1 :
return None
if not s1.islower():
return None
import string
letters_low =list(string.ascii_lowercase)
for x1 in letters_low:
for x2 in letters_low:
s1_copy=s1
s1_copy=s1_copy.replace(x1,x2)
num = 0
for i in range(len(s1)):
if s1_copy[i]!=s2[i]:
num+=1
list_num.append(num)
return min(list_num)