前言:
现时兄弟们对“js对比两段文字怎么弄”大致比较讲究,咱们都想要了解一些“js对比两段文字怎么弄”的相关知识。那么小编也在网摘上搜集了一些关于“js对比两段文字怎么弄””的相关资讯,希望大家能喜欢,同学们快快来了解一下吧!function similar(s, t, f) {
if (!s || !t) {
return 0
}
if(s === t){
return 100;
}
var l = s.length > t.length ? s.length : t.length
var n = s.length
var m = t.length
var d = []
f = f || 2
var min = function (a, b, c) {
return a < b ? (a < c ? a : c) : (b < c ? b : c)
}
var i, j, si, tj, cost
if (n === 0) return m
if (m === 0) return n
for (i = 0; i <= n; i++) {
d[i] = []
d[i][0] = i
}
for (j = 0; j <= m; j++) {
d[0][j] = j
}
for (i = 1; i <= n; i++) {
si = s.charAt(i - 1)
for (j = 1; j <= m; j++) {
tj = t.charAt(j - 1)
if (si === tj) {
cost = 0
} else {
cost = 1
}
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost)
}
}
let res = (1 - d[n][m] / l) *100
return res.toFixed(f)
}
标签: #js对比两段文字怎么弄