Add a recursive version of the Longest Common Substring. · polesye/javascript-algorithms@c9f1caf · GitHub
Skip to content

Commit c9f1caf

Browse files
committed
Add a recursive version of the Longest Common Substring.
1 parent bcd1cc1 commit c9f1caf

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Lines changed: 17 additions & 0 deletions
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‎

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

0 commit comments

Comments
 (0)