Added more implementations (JS) (#67)
* Added recursive binary search (JS) * Added recursive selection sorting * Added another loop implementation sum func by reduce (JS) * Recursion reduced by one iteration * Recursive binary search in ES6 added to appropriate folder * JS files ordered by standards (ES4/ES6) * Added hashtable implementation in JS * Fixed typo with LENGTH prop * Added Euclidian algorithm for two numbers and set of them * Added universal selection sort * Commented output * Added ES6 version of Euclidean algorithm * Converted from ES6 to ES5 * #69 Added search for LCS * #69 added levenstein algorithm * Removed excessive property * Removed excessive file * Removed excessive function calls * Removed excessive file * Removed excessive file * Renamed * Fixed indentation
This commit is contained in:
committed by
Aditya Bhargava
parent
a5fe9178dd
commit
0d5d0164ce
47
09_dynamic_programming/javascript/02_levenstein.js
Normal file
47
09_dynamic_programming/javascript/02_levenstein.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Compute the edit distance between the two given strings
|
||||
*
|
||||
* @param {*} source
|
||||
* @param {*} target
|
||||
*
|
||||
* @return {number}
|
||||
*/
|
||||
function getEditDistance(source, target) {
|
||||
if (source.length == 0) return target.length;
|
||||
if (target.length == 0) return source.length;
|
||||
|
||||
let i;
|
||||
let j;
|
||||
let matrix = [];
|
||||
|
||||
// Fill the column
|
||||
for (i = 0; i <= target.length; i++) {
|
||||
matrix[i] = [i];
|
||||
}
|
||||
|
||||
// Fill the column
|
||||
for (j = 0; j <= source.length; j++) {
|
||||
matrix[0][j] = j;
|
||||
}
|
||||
|
||||
// Fill in the rest of the matrix
|
||||
for (i = 1; i <= target.length; i++) {
|
||||
for (j = 1; j <= source.length; j++) {
|
||||
if (target.charAt(i - 1) == source.charAt(j - 1)) {
|
||||
matrix[i][j] = matrix[i - 1][j - 1];
|
||||
} else {
|
||||
matrix[i][j] = Math.min(
|
||||
matrix[i - 1][j - 1] + 1, // substitution
|
||||
Math.min(
|
||||
matrix[i][j - 1] + 1, // insertion
|
||||
matrix[i - 1][j] + 1
|
||||
)
|
||||
); // deletion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return matrix[target.length][source.length];
|
||||
}
|
||||
|
||||
console.log(getEditDistance("google", "notgoogl")); // 4
|
||||
Reference in New Issue
Block a user