Given an input string (s
) and a pattern (p
), implement regular expression matching with support for '.'
and '*'
.
The matching should cover the entire input string (not partial).
Note:
s
could be empty and contains only lowercase lettersa-z
.p
could be empty and contains only lowercase lettersa-z
, and characters like.
or*
.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Related Topics: Backtracking
、Dynamic Programming
、String
這題是要實作一個簡易的 Regular Expression 的 Parser,僅支援 a-z、.
和 *
,題目也限定了輸入會是這幾個字元,所以可以先不用考慮其他字元的情況。
這題我最先想到的是使用遞迴來解,只是判斷的情況有些瑣碎。實作步驟如下:
若 pattern 為空字串,則檢查 string 是否為空字串
檢查 pattern 的長度是否大於 2 且第二字元為 *
是,判斷 string 是否為空字串
.
的情況)
否,則判斷 string 的長度是否大於 0 且 pattern 的字首與 string 的字首是否相等
另外針對流程中幾個遞迴傳入的Case進行說明:
Case1: pattern 第二字元為 *
且 string 是為空字串,則呼叫遞迴傳入 string 與 pattern[2:]
*
可代表 pattern[0] 出現 0 次,因此略過 pattern 字首向後檢查。Case2:是,則呼叫遞分別傳入 string[1:] 與 pattern、string[1:] 與 pattern[2:] 和 string 與 pattern[2:]
*
出現多次的情況,下個字元比對時仍與 pattern 字首比對。*
出現 1 次的情況,因此下一個字元比對時略過 pattern 字首。*
出現 0 次的情況,因此忽略 pattern 字首。Case3: pattern 第二字元為 *
且 pattern 的字首與 string 的字首不相等,則呼叫遞迴傳入 string 與pattern[2:]
*
的緣故可以假定 pattern[0] 為不存在(出現 0 次)的元素,因此略過 pattern 字首向後檢查。不過單用遞迴會 Time Limit Exceeded,因此多加了參數暫存比較結果,節省呼叫次數。
果然效能好多了,Runtime: 52 ms, faster than 99.53% of Python3 online submissions forRegular Expression Matching.
是說寫完地回後很高興要切下一題的時候,才後知後覺的想到,我剛剛應該是挑了 DP 的標籤要練習阿 XDDD
但是,我真的對 DP 不太擅長的說,想破了頭還是毫無頭緒,只好去翻了下討論區的答案,解釋有點難懂還沒有啃透,晚點再回來補充好了…腦袋燒壞 Orz
P[i][j] = P[i - 1][j - 1]
, if p[j - 1] != '*' && (s[i - 1] == p[j - 1] || p[j - 1] == '.')
;P[i][j] = P[i][j - 2]
, if p[j - 1] == '*'
and the pattern repeats for 0
times;P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')
, if p[j - 1] == '*'
and the pattern repeats for at least 1
times.是說,我硬是將討論區的 Code 改成 Python 版本的,執行時間 60 ms,效能好像沒有比較好?
本文作者: 辛西亞.Cynthia
本文連結: 辛西亞的技能樹 / hackmd 版本
版權聲明: 部落格中所有文章,均採用 姓名標示-非商業性-相同方式分享 4.0 國際 (CC BY-NC-SA 4.0) 許可協議。轉載請標明作者、連結與出處!