diff --git a/09_dynamic_programming/ES6/01_longest_common_subsequence.js b/09_dynamic_programming/ES6/01_longest_common_subsequence.js index 366d23e..f23a7cf 100644 --- a/09_dynamic_programming/ES6/01_longest_common_subsequence.js +++ b/09_dynamic_programming/ES6/01_longest_common_subsequence.js @@ -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 diff --git a/09_dynamic_programming/ES6/examples/base.js b/09_dynamic_programming/ES6/examples/base.js deleted file mode 100644 index d50dda8..0000000 --- a/09_dynamic_programming/ES6/examples/base.js +++ /dev/null @@ -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; -}; diff --git a/09_dynamic_programming/ES6/examples/diff_two_words.js b/09_dynamic_programming/ES6/examples/diff_two_words.js deleted file mode 100644 index 1ac7d02..0000000 --- a/09_dynamic_programming/ES6/examples/diff_two_words.js +++ /dev/null @@ -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;