Fixed formatting problems, added JSDoc, deleted duplicate example

This commit is contained in:
Alexandrshy
2019-07-27 02:00:06 +04:00
parent bac32b613b
commit a76c5e5d7d
3 changed files with 58 additions and 68 deletions

View File

@@ -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++) {
matrix[i] = new Array(cols).fill(0);
for (let i = 0; i < rows; i++) {
matrix[i] = Array(columns).fill(0);
}
return matrix;
}
};
function substring(a, b) {
const cell = createMatrix(a.length + 1, b.length + 1);
let lcs = 0;
let lastSubIndex = 0;
/**
* Find the longest substring
* @param {string} firstWord First word
* @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 j = 1; j <= b.length; j++) {
if (a[i - 1] === b[j - 1]) {
cell[i][j] = cell[i - 1][j - 1] + 1;
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;
if (cell[i][j] > lcs) {
lcs = cell[i][j];
lastSubIndex = i;
if (matrix[i][j] >= sizeSequence) {
sizeSequence = matrix[i][j];
indexSequence = i + 1;
}
} 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"
substring("fish", "hish"); // "ish"
longestSubstring("vista", "hish"); // "is"
longestSubstring("fish", "hish"); // "ish"
function subsequence(a, b) {
const cell = createMatrix(a.length + 1, b.length + 1);
for (let i = 1; i <= a.length; i++) {
for (let j = 1; j <= b.length; j++) {
if (a[i] === b[j]) {
cell[i][j] = cell[i - 1][j - 1] + 1;
/**
* Find the longest common subsequence
* @param {string} firstWord First word
* @param {string} secondWord Second word
* @returns {number} The longest common subsequence
*/
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 {
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];
}
subsequence("fish", "fosh"); // 3
subsequence("fort", "fosh"); // 2
longestCommonSubsequence("fish", "fosh"); // 3
longestCommonSubsequence("fort", "fosh"); // 2

View File

@@ -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;
};

View File

@@ -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;