forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a recursive version of the Longest Common Substring.
- Loading branch information
Showing
4 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...gorithms/string/longest-common-substring/__test__/longestCommonSubstringRecursive.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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'); | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
src/algorithms/string/longest-common-substring/longestCommonSubstring.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/algorithms/string/longest-common-substring/longestCommonSubstringRecursive.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable no-param-reassign */ | ||
/** | ||
* Longest Common Substring (LCS) (Recursive Approach). | ||
* | ||
* @param {string} string1 | ||
* @param {string} string2 | ||
* @return {number} | ||
*/ | ||
export default function longestCommonSubstringRecursive(string1, string2) { | ||
/** | ||
* | ||
* @param {string} s1 | ||
* @param {string} s2 | ||
* @return {string} - returns the LCS (Longest Common Substring) | ||
*/ | ||
const lcs = (s1, s2, memo = {}) => { | ||
if (!s1 || !s2) return ''; | ||
|
||
if (memo[`${s1}:${s2}`]) return memo[`${s1}:${s2}`]; | ||
|
||
if (s1[0] === s2[0]) { | ||
return s1[0] + lcs(s1.substring(1), s2.substring(1), memo); | ||
} | ||
|
||
const nextLcs1 = lcs(s1.substring(1), s2, memo); | ||
const nextLcs2 = lcs(s1, s2.substring(1), memo); | ||
|
||
const nextLongest = nextLcs1.length >= nextLcs2.length ? nextLcs1 : nextLcs2; | ||
|
||
memo[`${s1}:${s2}`] = nextLongest; | ||
|
||
return nextLongest; | ||
}; | ||
|
||
return lcs(string1, string2); | ||
} |