2021-06-15 00:49:18 -07:00
---
id: 5e6dd1278e6ca105cde40ea9
2021-09-06 03:52:36 -07:00
title: Maior subsequência comum
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 385271
dashedName: longest-common-subsequence
---
# --description--
2021-09-06 03:52:36 -07:00
A **maior subsequência comum** (ou [**LCS** ](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem )) dos grupos A e B é o maior grupo de elementos de A e de B que são comuns entre os dois grupos e na mesma ordem em cada grupo. Por exemplo, as sequências "1234" e "1224533324" têm um LCS de "1234":
2021-06-15 00:49:18 -07:00
***1234***
***12***245***3***332***4***
2021-09-06 03:52:36 -07:00
Para um exemplo em string, considere as sequências "thisisatest" e "testing123testing". Uma LCS seria "tsitest":
2021-06-15 00:49:18 -07:00
***t***hi***si***sa***test***
***t***e***s***t***i***ng123***test***ing.
2021-09-06 03:52:36 -07:00
O código só precisa lidar com strings.
2021-06-15 00:49:18 -07:00
2021-09-06 03:52:36 -07:00
Para obter mais informações sobre esse problema, consulte a [Wikipedia ](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem ).
2021-06-15 00:49:18 -07:00
# --instructions--
2021-09-06 03:52:36 -07:00
Escreva uma função que diferencie maiúsculas de minúsculas e que retorne o LCS de duas strings. Você não precisa mostrar diversos LCS.
2021-06-15 00:49:18 -07:00
# --hints--
2021-09-06 03:52:36 -07:00
`lcs` deve ser uma função.
2021-06-15 00:49:18 -07:00
```js
assert(typeof lcs == 'function');
```
2021-09-06 03:52:36 -07:00
`lcs("thisisatest", "testing123testing")` deve retornar uma string.
2021-06-15 00:49:18 -07:00
```js
assert(typeof lcs('thisisatest', 'testing123testing') == 'string');
```
2021-09-06 03:52:36 -07:00
`lcs("thisisatest", "testing123testing")` deve retornar `"tsitest"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(lcs('thisisatest', 'testing123testing'), 'tsitest');
```
2021-09-06 03:52:36 -07:00
`lcs("ABCDGH", "AEDFHR")` deve retornar `"ADH"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(lcs('ABCDGH', 'AEDFHR'), 'ADH');
```
2021-09-06 03:52:36 -07:00
`lcs("AGGTAB", "GXTXAYB")` deve retornar `"GTAB"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(lcs('AGGTAB', 'GXTXAYB'), 'GTAB');
```
2021-09-06 03:52:36 -07:00
`lcs("BDACDB", "BDCB")` deve retornar `"BDCB"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(lcs('BDACDB', 'BDCB'), 'BDCB');
```
2021-09-06 03:52:36 -07:00
`lcs("ABAZDC", "BACBAD")` deve retornar `"ABAD"` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(lcs('ABAZDC', 'BACBAD'), 'ABAD');
```
# --seed--
## --seed-contents--
```js
function lcs(a, b) {
}
```
# --solutions--
```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;
}
}
```