Skip to content

Commit c9f1caf

Browse files
committedJan 30, 2023
Add a recursive version of the Longest Common Substring.
1 parent bcd1cc1 commit c9f1caf

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
 

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

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe('longestCommonSubstring', () => {
77
expect(longestCommonSubstring('', 'ABC')).toBe('');
88
expect(longestCommonSubstring('ABABC', 'BABCA')).toBe('BABC');
99
expect(longestCommonSubstring('BABCA', 'ABCBA')).toBe('ABC');
10+
expect(longestCommonSubstring('sea', 'eat')).toBe('ea');
11+
expect(longestCommonSubstring('algorithms', 'rithm')).toBe('rithm');
1012
expect(longestCommonSubstring(
1113
'Algorithms and data structures implemented in JavaScript',
1214
'Here you may find Algorithms and data structures that are implemented in JavaScript',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import longestCommonSubstring from '../longestCommonSubstringRecursive';
2+
3+
describe('longestCommonSubstringRecursive', () => {
4+
it('should find longest common substring between two strings', () => {
5+
expect(longestCommonSubstring('', '')).toBe('');
6+
expect(longestCommonSubstring('ABC', '')).toBe('');
7+
expect(longestCommonSubstring('', 'ABC')).toBe('');
8+
expect(longestCommonSubstring('ABABC', 'BABCA')).toBe('BABC');
9+
expect(longestCommonSubstring('BABCA', 'ABCBA')).toBe('ABCA');
10+
expect(longestCommonSubstring('sea', 'eat')).toBe('ea');
11+
expect(longestCommonSubstring('algorithms', 'rithm')).toBe('rithm');
12+
expect(longestCommonSubstring(
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/longestCommonSubstring.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/**
2+
* Longest Common Substring (LCS) (Dynamic Programming Approach).
3+
*
24
* @param {string} string1
35
* @param {string} string2
46
* @return {string}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable no-param-reassign */
2+
/**
3+
* Longest Common Substring (LCS) (Recursive Approach).
4+
*
5+
* @param {string} string1
6+
* @param {string} string2
7+
* @return {number}
8+
*/
9+
export default function longestCommonSubstringRecursive(string1, string2) {
10+
/**
11+
*
12+
* @param {string} s1
13+
* @param {string} s2
14+
* @return {string} - returns the LCS (Longest Common Substring)
15+
*/
16+
const lcs = (s1, s2, memo = {}) => {
17+
if (!s1 || !s2) return '';
18+
19+
if (memo[`${s1}:${s2}`]) return memo[`${s1}:${s2}`];
20+
21+
if (s1[0] === s2[0]) {
22+
return s1[0] + lcs(s1.substring(1), s2.substring(1), memo);
23+
}
24+
25+
const nextLcs1 = lcs(s1.substring(1), s2, memo);
26+
const nextLcs2 = lcs(s1, s2.substring(1), memo);
27+
28+
const nextLongest = nextLcs1.length >= nextLcs2.length ? nextLcs1 : nextLcs2;
29+
30+
memo[`${s1}:${s2}`] = nextLongest;
31+
32+
return nextLongest;
33+
};
34+
35+
return lcs(string1, string2);
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.