Skip to content

Commit c9f1caf

Browse files
committed
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‎

Lines changed: 2 additions & 0 deletions
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',
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
importlongestCommonSubstringfrom'../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‎

Lines changed: 2 additions & 0 deletions
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}
Lines changed: 36 additions & 0 deletions
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+
exportdefaultfunctionlongestCommonSubstringRecursive(string1,string2){
10+
/**
11+
*
12+
* @param{string} s1
13+
* @param{string} s2
14+
* @return{string} - returns the LCS (Longest Common Substring)
15+
*/
16+
constlcs=(s1,s2,memo={})=>{
17+
if(!s1||!s2)return'';
18+
19+
if(memo[`${s1}:${s2}`])returnmemo[`${s1}:${s2}`];
20+
21+
if(s1[0]===s2[0]){
22+
returns1[0]+lcs(s1.substring(1),s2.substring(1),memo);
23+
}
24+
25+
constnextLcs1=lcs(s1.substring(1),s2,memo);
26+
constnextLcs2=lcs(s1,s2.substring(1),memo);
27+
28+
constnextLongest=nextLcs1.length>=nextLcs2.length ? nextLcs1 : nextLcs2;
29+
30+
memo[`${s1}:${s2}`]=nextLongest;
31+
32+
returnnextLongest;
33+
};
34+
35+
returnlcs(string1,string2);
36+
}

0 commit comments

Comments
(0)