2.2 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5e6dd1278e6ca105cde40ea9 | Più lunga successione comune | 1 | 385271 | longest-common-subsequence |
--description--
The longest common subsequence (or LCS) 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
1224533324
Per usa stringa esempio, considera le sequenze thisisatest e testing123testing. Un LCS sarebbe tsitest:
thisisatest
testing123testing.
Il tuo codice deve riguardare solo le stringhe.
--instructions--
Scrivi una funzione sensibile alle maiuscole/minuscole che restituisce l'LCS di due stringhe. Non è necessario mostrare più LCS.
--hints--
lcs dovrebbe essere una funzione.
assert(typeof lcs == 'function');
lcs("thisisatest", "testing123testing") dovrebbe restituire una stringa.
assert(typeof lcs('thisisatest', 'testing123testing') == 'string');
lcs("thisisatest", "testing123testing") dovrebbe restituire "tsitest".
assert.equal(lcs('thisisatest', 'testing123testing'), 'tsitest');
lcs("ABCDGH", "AEDFHR") dovrebbe restituire "ADH".
assert.equal(lcs('ABCDGH', 'AEDFHR'), 'ADH');
lcs("AGGTAB", "GXTXAYB") dovrebbe restituire "GTAB".
assert.equal(lcs('AGGTAB', 'GXTXAYB'), 'GTAB');
lcs("BDACDB", "BDCB") dovrebbe restituire "BDCB".
assert.equal(lcs('BDACDB', 'BDCB'), 'BDCB');
lcs("ABAZDC", "BACBAD") dovrebbe restituire "ABAD".
assert.equal(lcs('ABAZDC', 'BACBAD'), 'ABAD');
--seed--
--seed-contents--
function lcs(a, b) {
}
--solutions--
function lcs(a, b) {
var aSub = a.substring(0, a.length - 1);
var bSub = b.substring(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;
}
}