From 5fc33c0f0ae5c6ccddaf8c40a8af483fb750a3ad Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 30 Jan 2023 09:08:17 +0100 Subject: [PATCH] Add a recursive version of the Longest Common Subsequence. --- .../longestCommonSubsequenceRecursive.test.js | 17 +++++++++++++++++ .../longestCommonSubsequenceRecursive.js} | 6 +++--- .../longestCommonSubstringRecursive.test.js | 17 ----------------- 3 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequenceRecursive.test.js rename src/algorithms/{string/longest-common-substring/longestCommonSubstringRecursive.js => sets/longest-common-subsequence/longestCommonSubsequenceRecursive.js} (77%) delete mode 100644 src/algorithms/string/longest-common-substring/__test__/longestCommonSubstringRecursive.test.js diff --git a/src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequenceRecursive.test.js b/src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequenceRecursive.test.js new file mode 100644 index 00000000..3d5c9d80 --- /dev/null +++ b/src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequenceRecursive.test.js @@ -0,0 +1,17 @@ +import longestCommonSubsequence from '../longestCommonSubsequenceRecursive'; + +describe('longestCommonSubsequenceRecursive', () => { + it('should find longest common substring between two strings', () => { + expect(longestCommonSubsequence('', '')).toBe(''); + expect(longestCommonSubsequence('ABC', '')).toBe(''); + expect(longestCommonSubsequence('', 'ABC')).toBe(''); + expect(longestCommonSubsequence('ABABC', 'BABCA')).toBe('BABC'); + expect(longestCommonSubsequence('BABCA', 'ABCBA')).toBe('ABCA'); + expect(longestCommonSubsequence('sea', 'eat')).toBe('ea'); + expect(longestCommonSubsequence('algorithms', 'rithm')).toBe('rithm'); + expect(longestCommonSubsequence( + 'Algorithms and data structures implemented in JavaScript', + 'Here you may find Algorithms and data structures that are implemented in JavaScript', + )).toBe('Algorithms and data structures implemented in JavaScript'); + }); +}); diff --git a/src/algorithms/string/longest-common-substring/longestCommonSubstringRecursive.js b/src/algorithms/sets/longest-common-subsequence/longestCommonSubsequenceRecursive.js similarity index 77% rename from src/algorithms/string/longest-common-substring/longestCommonSubstringRecursive.js rename to src/algorithms/sets/longest-common-subsequence/longestCommonSubsequenceRecursive.js index 88cb08aa..580ba8c7 100644 --- a/src/algorithms/string/longest-common-substring/longestCommonSubstringRecursive.js +++ b/src/algorithms/sets/longest-common-subsequence/longestCommonSubsequenceRecursive.js @@ -1,17 +1,17 @@ /* eslint-disable no-param-reassign */ /** - * Longest Common Substring (LCS) (Recursive Approach). + * Longest Common Subsequence (LCS) (Recursive Approach). * * @param {string} string1 * @param {string} string2 * @return {number} */ -export default function longestCommonSubstringRecursive(string1, string2) { +export default function longestCommonSubsequenceRecursive(string1, string2) { /** * * @param {string} s1 * @param {string} s2 - * @return {string} - returns the LCS (Longest Common Substring) + * @return {string} - returns the LCS (Longest Common Subsequence) */ const lcs = (s1, s2, memo = {}) => { if (!s1 || !s2) return ''; diff --git a/src/algorithms/string/longest-common-substring/__test__/longestCommonSubstringRecursive.test.js b/src/algorithms/string/longest-common-substring/__test__/longestCommonSubstringRecursive.test.js deleted file mode 100644 index d8428033..00000000 --- a/src/algorithms/string/longest-common-substring/__test__/longestCommonSubstringRecursive.test.js +++ /dev/null @@ -1,17 +0,0 @@ -import longestCommonSubstring from '../longestCommonSubstringRecursive'; - -describe('longestCommonSubstringRecursive', () => { - it('should find longest common substring between two strings', () => { - expect(longestCommonSubstring('', '')).toBe(''); - expect(longestCommonSubstring('ABC', '')).toBe(''); - expect(longestCommonSubstring('', 'ABC')).toBe(''); - expect(longestCommonSubstring('ABABC', 'BABCA')).toBe('BABC'); - expect(longestCommonSubstring('BABCA', 'ABCBA')).toBe('ABCA'); - expect(longestCommonSubstring('sea', 'eat')).toBe('ea'); - expect(longestCommonSubstring('algorithms', 'rithm')).toBe('rithm'); - expect(longestCommonSubstring( - 'Algorithms and data structures implemented in JavaScript', - 'Here you may find Algorithms and data structures that are implemented in JavaScript', - )).toBe('Algorithms and data structures implemented in JavaScript'); - }); -});