Merge pull request #143 from Alexandrshy/issues-142-formatting-problems-and-example-for-dynamic-programming
Fixed formatting problems, added JSDoc, deleted duplicate example
This commit is contained in:
@@ -1,54 +1,78 @@
|
|||||||
function createMatrix(rows, cols) {
|
/**
|
||||||
const matrix = new Array(rows);
|
* Create a matrix
|
||||||
|
* @param {number} rows Number of rows
|
||||||
|
* @param {number} columns ANumber of columns
|
||||||
|
* @returns {Array} Matrix
|
||||||
|
*/
|
||||||
|
const createMatrix = (rows = 0, columns = 0) => {
|
||||||
|
const matrix = [];
|
||||||
|
|
||||||
for (let i = 0; i < matrix.length; i++) {
|
for (let i = 0; i < rows; i++) {
|
||||||
matrix[i] = new Array(cols).fill(0);
|
matrix[i] = Array(columns).fill(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
};
|
||||||
|
|
||||||
function substring(a, b) {
|
/**
|
||||||
const cell = createMatrix(a.length + 1, b.length + 1);
|
* Find the longest substring
|
||||||
let lcs = 0;
|
* @param {string} firstWord First word
|
||||||
let lastSubIndex = 0;
|
* @param {string} secondWord Second word
|
||||||
|
* @returns {string} The longest substring
|
||||||
|
*/
|
||||||
|
const longestSubstring = (firstWord = "", secondWord = "") => {
|
||||||
|
const matrix = JSON.parse(
|
||||||
|
JSON.stringify(createMatrix(firstWord.length, secondWord.length))
|
||||||
|
);
|
||||||
|
let sizeSequence = 0;
|
||||||
|
let indexSequence = 0;
|
||||||
|
|
||||||
for (let i = 1; i <= a.length; i++) {
|
for (let i = 0; i < firstWord.length; i++) {
|
||||||
for (let j = 1; j <= b.length; j++) {
|
for (let j = 0; j < secondWord.length; j++) {
|
||||||
if (a[i - 1] === b[j - 1]) {
|
if (firstWord[i] === secondWord[j]) {
|
||||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
matrix[i][j] = (i && j) > 0 ? matrix[i - 1][j - 1] + 1 : 1;
|
||||||
|
|
||||||
if (cell[i][j] > lcs) {
|
if (matrix[i][j] >= sizeSequence) {
|
||||||
lcs = cell[i][j];
|
sizeSequence = matrix[i][j];
|
||||||
lastSubIndex = i;
|
indexSequence = i + 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cell[i][j] = 0;
|
matrix[i][j] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return a.slice(lastSubIndex - lcs, lastSubIndex);
|
return firstWord.slice(indexSequence - sizeSequence, indexSequence);
|
||||||
}
|
};
|
||||||
|
|
||||||
substring("vista", "hish"); // "is"
|
longestSubstring("vista", "hish"); // "is"
|
||||||
substring("fish", "hish"); // "ish"
|
longestSubstring("fish", "hish"); // "ish"
|
||||||
|
|
||||||
function subsequence(a, b) {
|
/**
|
||||||
const cell = createMatrix(a.length + 1, b.length + 1);
|
* Find the longest common subsequence
|
||||||
|
* @param {string} firstWord First word
|
||||||
for (let i = 1; i <= a.length; i++) {
|
* @param {string} secondWord Second word
|
||||||
for (let j = 1; j <= b.length; j++) {
|
* @returns {number} The longest common subsequence
|
||||||
if (a[i] === b[j]) {
|
*/
|
||||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
const longestCommonSubsequence = (firstWord = "", secondWord = "") => {
|
||||||
|
const matrix = JSON.parse(
|
||||||
|
JSON.stringify(createMatrix(firstWord.length, secondWord.length))
|
||||||
|
);
|
||||||
|
if (matrix.length === 0 || matrix[0].length === 0) return 0;
|
||||||
|
for (let i = 0; i < firstWord.length; i++) {
|
||||||
|
for (let j = 0; j < secondWord.length; j++) {
|
||||||
|
if (firstWord[i] === secondWord[j]) {
|
||||||
|
matrix[i][j] = (i && j) > 0 ? matrix[i - 1][j - 1] + 1 : 1;
|
||||||
} else {
|
} else {
|
||||||
cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]);
|
matrix[i][j] = Math.max(
|
||||||
|
i > 0 ? matrix[i - 1][j] : 0,
|
||||||
|
j > 0 ? matrix[i][j - 1] : 0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return matrix[firstWord.length - 1][secondWord.length - 1];
|
||||||
|
};
|
||||||
|
|
||||||
return cell[a.length][b.length];
|
longestCommonSubsequence("fish", "fosh"); // 3
|
||||||
}
|
longestCommonSubsequence("fort", "fosh"); // 2
|
||||||
|
|
||||||
subsequence("fish", "fosh"); // 3
|
|
||||||
subsequence("fort", "fosh"); // 2
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
export const initializeMatrix = (rows, cols) => {
|
|
||||||
const matrix = [];
|
|
||||||
for (let i = 0; i < rows.length; i += 1) {
|
|
||||||
matrix.push(Array(cols.length).fill(0));
|
|
||||||
}
|
|
||||||
return matrix;
|
|
||||||
};
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
import { initializeMatrix } from "./base";
|
|
||||||
|
|
||||||
const diff = (firstWord, secondWord) => {
|
|
||||||
const arr1 = firstWord.split("");
|
|
||||||
const arr2 = secondWord.split("");
|
|
||||||
const matrix = initializeMatrix(arr1, arr2);
|
|
||||||
for (let i = 0; i < arr1.length; i += 1) {
|
|
||||||
for (let j = 0; j < arr2.length; j += 1) {
|
|
||||||
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];
|
|
||||||
};
|
|
||||||
|
|
||||||
export default diff;
|
|
||||||
Reference in New Issue
Block a user