Skip to content

Commit 5fc33c0

Browse files
committedJan 30, 2023
Add a recursive version of the Longest Common Subsequence.
1 parent c9f1caf commit 5fc33c0

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import longestCommonSubsequence from '../longestCommonSubsequenceRecursive';
2+
3+
describe('longestCommonSubsequenceRecursive', () => {
4+
it('should find longest common substring between two strings', () => {
5+
expect(longestCommonSubsequence('', '')).toBe('');
6+
expect(longestCommonSubsequence('ABC', '')).toBe('');
7+
expect(longestCommonSubsequence('', 'ABC')).toBe('');
8+
expect(longestCommonSubsequence('ABABC', 'BABCA')).toBe('BABC');
9+
expect(longestCommonSubsequence('BABCA', 'ABCBA')).toBe('ABCA');
10+
expect(longestCommonSubsequence('sea', 'eat')).toBe('ea');
11+
expect(longestCommonSubsequence('algorithms', 'rithm')).toBe('rithm');
12+
expect(longestCommonSubsequence(
13+
'Algorithms and data structures implemented in JavaScript',
14+
'Here you may find Algorithms and data structures that are implemented in JavaScript',
15+
)).toBe('Algorithms and data structures implemented in JavaScript');
16+
});
17+
});

‎src/algorithms/string/longest-common-substring/longestCommonSubstringRecursive.js ‎src/algorithms/sets/longest-common-subsequence/longestCommonSubsequenceRecursive.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/* eslint-disable no-param-reassign */
22
/**
3-
* Longest Common Substring (LCS) (Recursive Approach).
3+
* Longest Common Subsequence (LCS) (Recursive Approach).
44
*
55
* @param {string} string1
66
* @param {string} string2
77
* @return {number}
88
*/
9-
export default function longestCommonSubstringRecursive(string1, string2) {
9+
export default function longestCommonSubsequenceRecursive(string1, string2) {
1010
/**
1111
*
1212
* @param {string} s1
1313
* @param {string} s2
14-
* @return {string} - returns the LCS (Longest Common Substring)
14+
* @return {string} - returns the LCS (Longest Common Subsequence)
1515
*/
1616
const lcs = (s1, s2, memo = {}) => {
1717
if (!s1 || !s2) return '';

‎src/algorithms/string/longest-common-substring/__test__/longestCommonSubstringRecursive.test.js

-17
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.