mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add a recursive version of the Longest Common Subsequence.
This commit is contained in:
parent
c9f1caf1ca
commit
5fc33c0f0a
@ -0,0 +1,17 @@
|
|||||||
|
import longestCommonSubsequence from '../longestCommonSubsequenceRecursive';
|
||||||
|
|
||||||
|
describe('longestCommonSubsequenceRecursive', () => {
|
||||||
|
it('should find longest common substring between two strings', () => {
|
||||||
|
expect(longestCommonSubsequence('', '')).toBe('');
|
||||||
|
expect(longestCommonSubsequence('ABC', '')).toBe('');
|
||||||
|
expect(longestCommonSubsequence('', 'ABC')).toBe('');
|
||||||
|
expect(longestCommonSubsequence('ABABC', 'BABCA')).toBe('BABC');
|
||||||
|
expect(longestCommonSubsequence('BABCA', 'ABCBA')).toBe('ABCA');
|
||||||
|
expect(longestCommonSubsequence('sea', 'eat')).toBe('ea');
|
||||||
|
expect(longestCommonSubsequence('algorithms', 'rithm')).toBe('rithm');
|
||||||
|
expect(longestCommonSubsequence(
|
||||||
|
'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');
|
||||||
|
});
|
||||||
|
});
|
@ -1,17 +1,17 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
/**
|
/**
|
||||||
* Longest Common Substring (LCS) (Recursive Approach).
|
* Longest Common Subsequence (LCS) (Recursive Approach).
|
||||||
*
|
*
|
||||||
* @param {string} string1
|
* @param {string} string1
|
||||||
* @param {string} string2
|
* @param {string} string2
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
export default function longestCommonSubstringRecursive(string1, string2) {
|
export default function longestCommonSubsequenceRecursive(string1, string2) {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {string} s1
|
* @param {string} s1
|
||||||
* @param {string} s2
|
* @param {string} s2
|
||||||
* @return {string} - returns the LCS (Longest Common Substring)
|
* @return {string} - returns the LCS (Longest Common Subsequence)
|
||||||
*/
|
*/
|
||||||
const lcs = (s1, s2, memo = {}) => {
|
const lcs = (s1, s2, memo = {}) => {
|
||||||
if (!s1 || !s2) return '';
|
if (!s1 || !s2) return '';
|
@ -1,17 +0,0 @@
|
|||||||
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');
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user