Skip to content

Commit 15e798c

Browse files
committed
Update README.
1 parent 01139e0 commit 15e798c

File tree

5 files changed

+52
-34
lines changed

5 files changed

+52
-34
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/power-set) - all subsets of the set
3939
* [Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/permutations) (with and without repetitions)
4040
* [Combinations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/combinations) (with and without repetitions)
41-
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/fisher-yates) - random permutation of a finite sequence
41+
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/fisher-yates) - random permutation of a finite sequence
42+
* [Longest Common Subsequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/longest-common-subsequnce) (LCS)
4243
* **String**
4344
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
4445
* [Hamming Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
4546
* [Knuth–Morris–Pratt Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/knuth-morris-pratt) - substring search
4647
* [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search
47-
* [Longest Common Subsequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-subsequnce) (LCS)
4848
* [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
4949
* **Search**
5050
* [Binary Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/binary-search)
@@ -90,7 +90,7 @@
9090
* **Dynamic Programming**
9191
* [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci)
9292
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
93-
* [Longest Common Subsequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-subsequnce) (LCS)
93+
* [Longest Common Subsequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/longest-common-subsequnce) (LCS)
9494
* [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
9595
* Increasing subsequence
9696
* Longest Increasing subsequence
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import longestCommonSubsequnce from '../longestCommonSubsequnce';
2+
3+
describe('longestCommonSubsequnce', () => {
4+
it('should find longest common subsequence for two strings', () => {
5+
expect(longestCommonSubsequnce([''], [''])).toEqual(['']);
6+
7+
expect(longestCommonSubsequnce([''], ['A', 'B', 'C'])).toEqual(['']);
8+
9+
expect(longestCommonSubsequnce(['A', 'B', 'C'], [''])).toEqual(['']);
10+
11+
expect(longestCommonSubsequnce(
12+
['A', 'B', 'C'],
13+
['D', 'E', 'F', 'G'],
14+
)).toEqual(['']);
15+
16+
expect(longestCommonSubsequnce(
17+
['A', 'B', 'C', 'D', 'G', 'H'],
18+
['A', 'E', 'D', 'F', 'H', 'R'],
19+
)).toEqual(['A', 'D', 'H']);
20+
21+
expect(longestCommonSubsequnce(
22+
['A', 'G', 'G', 'T', 'A', 'B'],
23+
['G', 'X', 'T', 'X', 'A', 'Y', 'B'],
24+
)).toEqual(['G', 'T', 'A', 'B']);
25+
26+
expect(longestCommonSubsequnce(
27+
['A', 'B', 'C', 'D', 'A', 'F'],
28+
['A', 'C', 'B', 'C', 'F'],
29+
)).toEqual(['A', 'B', 'C', 'F']);
30+
});
31+
});
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/**
2-
* @param {string} s1
3-
* @param {string} s2
4-
* @return {string}
2+
* @param {string[]} set1
3+
* @param {string[]} set2
4+
* @return {string[]}
55
*/
6-
export default function longestCommonSubsequnce(s1, s2) {
6+
export default function longestCommonSubsequnce(set1, set2) {
77
// Init LCS matrix.
8-
const lcsMatrix = Array(s2.length + 1).fill(null).map(() => Array(s1.length + 1).fill(null));
8+
const lcsMatrix = Array(set2.length + 1).fill(null).map(() => Array(set1.length + 1).fill(null));
99

1010
// Fill first row with zeros.
11-
for (let columnIndex = 0; columnIndex <= s1.length; columnIndex += 1) {
11+
for (let columnIndex = 0; columnIndex <= set1.length; columnIndex += 1) {
1212
lcsMatrix[0][columnIndex] = 0;
1313
}
1414

1515
// Fill first column with zeros.
16-
for (let rowIndex = 0; rowIndex <= s2.length; rowIndex += 1) {
16+
for (let rowIndex = 0; rowIndex <= set2.length; rowIndex += 1) {
1717
lcsMatrix[rowIndex][0] = 0;
1818
}
1919

2020
// Fill rest of the column that correspond to each of two strings.
21-
for (let rowIndex = 1; rowIndex <= s2.length; rowIndex += 1) {
22-
for (let columnIndex = 1; columnIndex <= s1.length; columnIndex += 1) {
23-
if (s1[columnIndex - 1] === s2[rowIndex - 1]) {
21+
for (let rowIndex = 1; rowIndex <= set2.length; rowIndex += 1) {
22+
for (let columnIndex = 1; columnIndex <= set1.length; columnIndex += 1) {
23+
if (set1[columnIndex - 1] === set2[rowIndex - 1]) {
2424
lcsMatrix[rowIndex][columnIndex] = lcsMatrix[rowIndex - 1][columnIndex - 1] + 1;
2525
} else {
2626
lcsMatrix[rowIndex][columnIndex] = Math.max(
@@ -32,19 +32,19 @@ export default function longestCommonSubsequnce(s1, s2) {
3232
}
3333

3434
// Calculate LCS based on LCS matrix.
35-
if (!lcsMatrix[s2.length][s1.length]) {
35+
if (!lcsMatrix[set2.length][set1.length]) {
3636
// If the length of largest common string is zero then return empty string.
37-
return '';
37+
return [''];
3838
}
3939

40-
let lcs = '';
41-
let columnIndex = s1.length;
42-
let rowIndex = s2.length;
40+
const longestSequence = [];
41+
let columnIndex = set1.length;
42+
let rowIndex = set2.length;
4343

4444
while (columnIndex > 0 || rowIndex > 0) {
45-
if (s1[columnIndex - 1] === s2[rowIndex - 1]) {
45+
if (set1[columnIndex - 1] === set2[rowIndex - 1]) {
4646
// Move by diagonal left-top.
47-
lcs = s1[columnIndex - 1] + lcs;
47+
longestSequence.unshift(set1[columnIndex - 1]);
4848
columnIndex -= 1;
4949
rowIndex -= 1;
5050
} else if (lcsMatrix[rowIndex][columnIndex] === lcsMatrix[rowIndex][columnIndex - 1]) {
@@ -56,5 +56,5 @@ export default function longestCommonSubsequnce(s1, s2) {
5656
}
5757
}
5858

59-
return lcs;
59+
return longestSequence;
6060
}

src/algorithms/string/longest-common-subsequnce/__test__/longestCommonSubsequnce.test.js

-13
This file was deleted.

0 commit comments

Comments
 (0)