//获取两个字符的距离 function getDis(s1,s2) { var n=s1.length,m=s2.length;//长度 var n1=0,n2=0;//扫描点 let dis=0; //开始扫描 while (n1<n&&n2<m){ //相等 if(s1[n1]===s2[n2]){ let nn1=n1+1; let nn2=n2+1; while (nn1<n&&nn2<m&&s1[nn1]===s2[nn2]){ nn1++; nn2++; } n1=nn1; n2=nn2; }else{ let d1=0; let d2=0; if(n1+1<n){ d2=1; } if(n2+1<m){ d1=1; } //求不等距离 if(d1===1&&d2===1){ while (n2 + d1 < m && s1[n1] !== s2[n2 + d1]) { d1++; } while (n1 + d2 < n && s1[n1 + d2] !== s2[n2]) { d2++; } } if(n-n1-d2<m-n2-d1){ //增加 dis=dis+1 n2=n2+1; }else if(n-n1-d2>m-n2-d1){ //删除 dis=dis+1 n1=n1+1; } else{ //替换 dis=dis+2 n1=n1+1; n2=n2+1; } } //超出的处理 if(n1>=n&&n2<m){ //增加 dis=dis+m-n2; n2=m; } if(n2>=m&&n1<n){ //删除 dis=dis+n-n1; n1=n } } return dis; } console.log(getDis('abcdsedfsdfeeefabcdsedfsdfeeef','abcdeeeefabcdeeeef'))