150 najlepszych wywiadówŚredni

Dopasowywanie wyrażeń regularnych

Szczegółowy przewodnik i implementacja Python dla problemu „Dopasowywanie wyrażeń regularnych”.

Oświadczenie o problemie

Średni

Biorąc pod uwagę ciąg wejściowy s i wzorzec p, zaimplementuj dopasowywanie wyrażeń regularnych z obsługą „.” i „*” gdzie:

- „.” Pasuje do dowolnego pojedynczego znaku.

- '*' Dopasowuje zero lub więcej poprzedniego elementu.

Dopasowanie powinno obejmować cały ciąg wejściowy (nie częściowy).

Napisz funkcję isMatch(s: str, p: str) -> bool.

Ograniczenia
  • 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.

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla 2D DP, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.