Wawancara 150 TeratasSedang

Pencocokan Ekspresi Reguler

Panduan terperinci dan implementasi Python untuk masalah 'Pencocokan Ekspresi Reguler'.

Pernyataan Masalah

Sedang

Dengan adanya string masukan s dan pola p, terapkan pencocokan ekspresi reguler dengan dukungan untuk '.' dan '*' di mana:

- '.' Cocok dengan karakter apa pun.

- '*' Cocok dengan nol atau lebih elemen sebelumnya.

Pencocokannya harus mencakup seluruh string masukan (bukan sebagian).

Tulis fungsi isMatch(s: str, p: str) -> bool.

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

Contoh

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?
Pertimbangkan untuk menggunakan struktur data khusus DP 2D seperti kumpulan atau tumpukan.
Edge Cases to Watch
  • Struktur masukan kosong
  • Masukan elemen tunggal
  • Batasan angka yang besar

Siap Memecahkannya?

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

Buka di 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

Sumber Daya Python yang Direkomendasikan

Perluas pengetahuan Anda dengan tutorial interaktif terkait, lembar contekan, dan perbandingan kode.