Top 150 Interview

Regular Expression Matching

Detailed guide and Python implementation for the 'Regular Expression Matching' problem.

問題提起

Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:

- '.' Matches any single character.

- '*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Write a function isMatch(s: str, p: str) -> bool.

制約
  • 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.

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?
Consider using 2D DP-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

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

エディタで開く
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

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。