Calculate the similarity percentage between two strings based on edit distance.
Code
Algorithmsconst m = a.length, n = b.length;
const dp = Array.from({ length: m + 1 }, (_, i) =>
Array.from({ length: n + 1 }, (_, j) => i === 0 ? j : j === 0 ? i : 0)
);
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
dp[i][j] = a[i-1] === b[j-1] ? dp[i-1][j-1] : 1 + Math.min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]);
}
}
return Math.round((1 - dp[m][n] / Math.max(m, n)) * 100);Parameters
First string
Second string
Browser·fetch() may be limited by CORS
More JavaScript Snippets
Capitalize First Letter
Capitalize the first letter of a string while keeping the rest unchanged.
Center String
Center a string within a given width by padding with spaces.
Check if Palindrome
Check if a string is a palindrome by comparing characters from both ends.
Compare Strings (Case Insensitive)
Compare two strings ignoring case differences.
Convert to Camel Case
Convert a string to camelCase format with lowercase first letter.
Convert to Kebab Case
Convert a string to kebab-case format.