reorg and add code for second edition

This commit is contained in:
Aditya Bhargava
2023-08-09 08:20:19 -05:00
parent 9306432a1b
commit 933acafaf3
89 changed files with 18 additions and 117 deletions

View File

@@ -0,0 +1,78 @@
/**
* Search for LCS
*
* @param {string} string1 first string
* @param {string} string2 second string
*
* @return {object} with keys: lcs, offset, sequence
*/
function lcs(string1, string2) {
if (!string1 || !string2) {
return {
lcs: 0,
offset: 0,
sequence: ""
};
}
let lcs = 0;
let lastSubIndex = 0;
let table = [];
let len1 = string1.length;
let len2 = string2.length;
let row;
let col;
/**
* Matrix
* - has an increased dimension to avoid extra checks for previous elements
*
* - the number of rows is equal to the length of the first string + 1
* - the number of columns is equal to the length of the second string + 1
*/
for (row = 0; row <= len1; row++) {
table[row] = [];
for (col = 0; col <= len2; col++) {
table[row][col] = 0;
}
}
// Fill the matrix
let i;
let j;
for (i = 0; i < len1; i++) {
for (j = 0; j < len2; j++) {
if (string1[i] === string2[j]) {
// The letters match
if (table[i][j] === 0) {
table[i + 1][j + 1] = 1;
} else {
table[i + 1][j + 1] = table[i][j] + 1;
}
// increment lcs if it's needed
if (table[i + 1][j + 1] > lcs) {
lcs = table[i + 1][j + 1];
lastSubIndex = i;
}
} else {
// The letters don't match
table[i + 1][j + 1] = 0;
}
}
}
return {
lcs: lcs,
offset: lastSubIndex - lcs + 1,
sequence: string1.slice(lastSubIndex - lcs + 1, lastSubIndex + 1)
};
}
console.log(lcs("hish", "fish")); // { lcs: 3, offset: 1, sequence: 'ish' }
console.log(lcs("vista", "hish")); // { lcs: 2, offset: 1, sequence: 'is' }
console.log(lcs("google", "abcdefgooglehijklm")); // { lcs: 6, offset: 0, sequence: 'google' }
console.log(lcs("0", 0)); // { lcs: 0, offset: 0, sequence: '' }

View 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

View File

@@ -0,0 +1,7 @@
module.exports = function initialize_matrix(rows, cols) {
let matrix = [];
for (let i = 0; i < rows.length; i++) {
matrix.push(Array(cols.length).fill(0));
}
return matrix;
};

View File

@@ -0,0 +1,25 @@
const initialize_matrix = require("./base.js");
function diff(firstWord, secondWord) {
let arr1 = firstWord.split("");
let arr2 = secondWord.split("");
let matrix = initialize_matrix(arr1, arr2);
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
if (i > 0 && j > 0) {
matrix[i][j] = matrix[i - 1][j - 1] + 1;
} else {
matrix[i][j] = 1;
}
} else {
if (i > 0 && j > 0) {
matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
} else {
matrix[i][j] = 0;
}
}
}
}
return matrix[arr1.length - 1][arr2.length - 1];
}