Le migliori 150 intervisteMedio

Corrispondenza delle espressioni regolari

Guida dettagliata e implementazione Python per il problema della "corrispondenza delle espressioni regolari".

Dichiarazione del problema

Medio

Data una stringa di input s e un modello p, implementa la corrispondenza delle espressioni regolari con il supporto per '.' e '*' dove:

-'.' Corrisponde a qualsiasi singolo carattere.

- '*' Corrisponde a zero o più elementi precedenti.

La corrispondenza dovrebbe coprire l'intera stringa di input (non parziale).

Scrivi una funzione isMatch(s: str, p: str) -> bool.

Vincoli
  • 1 <= len(s) <= 20
  • 1 <= len(p) <= 20
  • s contains only lowercase English letters.
  • p contains only lowercase English letters, '.', and '*'.
  • It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.

Esempi

Example 1
Input
s = "aa", p = "a"
Output
False
Explanation

"a" does not match the entire string "aa".

Example 2
Input
s = "aa", p = "a*"
Output
True
Explanation

'*' repeats the preceding 'a' once to match "aa".

Example 3
Input
s = "ab", p = ".*"
Output
True
Explanation

".*" matches zero or more of any character.

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.