Programmazione competitivaFacile

Sequenza comune più lunga

Guida dettagliata e implementazione Python per il problema della "sequenza comune più lunga".

Dichiarazione del problema

Facile

Scrivi una funzione lcs(s1, s2) che restituisca la lunghezza della sottosequenza comune più lunga di due stringhe s1 e s2.

Vincoli
  • 0 <= len(s1), len(s2) <= 1000
  • Strings consist of uppercase English letters.

Esempi

Example 1
Input
lcs('ABCDGH', 'AEDFHR')
Output
3
Explanation

The longest common subsequence is 'ADH' which has length 3.

Example 2
Input
lcs('AGGTAB', 'GXTXAYB')
Output
4
Explanation

The longest common subsequence is 'GTAB' which has length 4.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche della programmazione dinamica come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.