Skip to content

Commit

Permalink
Add a recursive version of the Longest Common Substring.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Jan 30, 2023
1 parent bcd1cc1 commit c9f1caf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe('longestCommonSubstring', () => {
expect(longestCommonSubstring('', 'ABC')).toBe('');
expect(longestCommonSubstring('ABABC', 'BABCA')).toBe('BABC');
expect(longestCommonSubstring('BABCA', 'ABCBA')).toBe('ABC');
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',
Expand Down
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');
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/**
* Longest Common Substring (LCS) (Dynamic Programming Approach).
*
* @param {string} string1
* @param {string} string2
* @return {string}
Expand Down
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);
}

0 comments on commit c9f1caf

Please sign in to comment.