Le migliori 150 intervisteMedio

Sottosequenze distinte

Guida dettagliata e implementazione Python per il problema delle "sottosequenze distinte".

Dichiarazione del problema

Medio

Date due stringhe s e t, restituisce il numero di sottosequenze distinte di s che è uguale a t.

La sottosequenza di una stringa è una nuova stringa formata dalla stringa originale eliminando alcuni (non può essere nessuno) dei caratteri senza disturbare le posizioni relative dei caratteri rimanenti. (vale a dire, "ACE" è una sottosequenza di "ABCDE" mentre "AEC" non lo è).

Scrivi una funzione numDistinct(s: str, t: str) -> int.

Vincoli
  • 1 <= len(s), len(t) <= 1000
  • s and t consist of English letters

Esempi

Example 1
Input
s = "rabbbit", t = "rabbit"
Output
3
Explanation

There are 3 ways you can generate "rabbit" from s: **rab**b**bit**, **ra**b**bbit**, **rab**bb**it**.

Example 2
Input
s = "babgbag", t = "bag"
Output
5
Explanation

There are 5 ways you can generate "bag" from s.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per DP 2D 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.