2020-04-21 04:20:40 +00:00
---
id: 5e6dd1278e6ca105cde40ea9
title: Longest common subsequence
challengeType: 5
2020-06-08 14:37:41 +00:00
forumTopicId: 385271
2021-01-13 02:31:00 +00:00
dashedName: longest-common-subsequence
2020-04-21 04:20:40 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
The **longest common subsequence** (or [**LCS** ](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem )) of groups A and B is the longest group of elements from A and B that are common between the two groups and in the same order in each group. For example, the sequences "1234" and "1224533324" have an LCS of "1234":
***1234***
***12***245***3***332***4***
2020-04-21 04:20:40 +00:00
For a string example, consider the sequences "thisisatest" and "testing123testing". An LCS would be "tsitest":
2020-11-27 18:02:05 +00:00
***t***hi***si***sa***test***
***t***e***s***t***i***ng123***test***ing.
2020-04-21 04:20:40 +00:00
Your code only needs to deal with strings.
2020-11-27 18:02:05 +00:00
For more information on this problem please see [Wikipedia ](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem ).
# --instructions--
2020-04-21 04:20:40 +00:00
Write a case-sensitive function that returns the LCS of two strings. You don't need to show multiple LCS's.
2020-11-27 18:02:05 +00:00
# --hints--
`lcs` should be a function.
```js
assert(typeof lcs == 'function');
2020-04-21 04:20:40 +00:00
```
2020-11-27 18:02:05 +00:00
`lcs("thisisatest", "testing123testing")` should return a string.
2020-04-21 04:20:40 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof lcs('thisisatest', 'testing123testing') == 'string');
```
2020-04-21 04:20:40 +00:00
2020-11-27 18:02:05 +00:00
`lcs("thisisatest", "testing123testing")` should return `"tsitest"` .
2020-04-21 04:20:40 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.equal(lcs('thisisatest', 'testing123testing'), 'tsitest');
```
2020-06-08 14:37:41 +00:00
2020-11-27 18:02:05 +00:00
`lcs("ABCDGH", "AEDFHR")` should return `"ADH"` .
```js
assert.equal(lcs('ABCDGH', 'AEDFHR'), 'ADH');
2020-04-21 04:20:40 +00:00
```
2020-11-27 18:02:05 +00:00
`lcs("AGGTAB", "GXTXAYB")` should return `"GTAB"` .
2020-04-21 04:20:40 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.equal(lcs('AGGTAB', 'GXTXAYB'), 'GTAB');
```
`lcs("BDACDB", "BDCB")` should return `"BDCB"` .
```js
assert.equal(lcs('BDACDB', 'BDCB'), 'BDCB');
```
2020-04-21 04:20:40 +00:00
2020-11-27 18:02:05 +00:00
`lcs("ABAZDC", "BACBAD")` should return `"ABAD"` .
```js
assert.equal(lcs('ABAZDC', 'BACBAD'), 'ABAD');
```
# --seed--
## --seed-contents--
```js
function lcs(a, b) {
}
```
# --solutions--
2020-04-21 04:20:40 +00:00
```js
function lcs(a, b) {
var aSub = a.substr(0, a.length - 1);
var bSub = b.substr(0, b.length - 1);
if (a.length === 0 || b.length === 0) {
return '';
} else if (a.charAt(a.length - 1) === b.charAt(b.length - 1)) {
return lcs(aSub, bSub) + a.charAt(a.length - 1);
} else {
var x = lcs(a, bSub);
var y = lcs(aSub, b);
return (x.length > y.length) ? x : y;
}
}
```