[Leetcode] Regular expression matching 正則表達式匹配
Implement regular expression matching with support for‘.‘and‘*‘.
‘.‘ Matches any single character. ‘*‘ Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true
題意:‘ . ‘能匹配任意字符,‘ * ‘表示之前的那個字符可以是0個、1個或者多個,(註意:s= ba和 p= a*bc也是匹配的,*表示p中 * 之前的字符為0個,但s=‘bc’和 p=‘aa*bc’是不匹配的)。
思路:根據字符‘ * ‘的特殊性,整體的解決方法主要是分兩種情況:
一、p的第二個字符*(p+1)不是 ‘ * ‘,這種情況,只要存在 *s==*p 或者*p=‘ . ‘中的一種情況,就說明當前p 和 s 對應的字符匹配,就可以比較兩個的下一個字符(這有一個前提,就是 s 要不為空,要是 s 為空了,就不匹配了,不用繼續比較了);
二、p的第二個字符*(p+1)是 ‘ * ‘,這種情況就比較麻煩了,也分兩種情況:
1) 在*s==*p 或者*p==‘ . ‘其中的一種情況下,判斷 ‘ * ‘是代表0個、1個或者多個前一個字符,如何去實現了?先將 *s 和*(p+2)去匹配,看是否能匹配,若能代表*表示0個之前字符,若是不能,則將s++,然後和P接著匹配,看是否匹配,若是,則將 *s 和*(p+2)去匹配繼續上部分的循環;若不是,則直接將 *s 和*(p+2)去匹配,不用繼續判斷*s和*p是否匹配。是有點繞口,結合代碼看,可能稍微好些。如:s= aba和 p= a*ba或者s= aab和 p= a*bc
2)*s !=*p 和*p !=‘ . ‘,說明,p中的第一個字符,在s中不存在,直接說明 ‘ * ‘代表0個之前的字符,那麽就繼續判斷 *s 和*(p+2)是否匹配。如s= ba和 p= a*bc是匹配,s= ba和 p= a*cbc則是不匹配,但是說明依舊說明在開始判斷時,‘ * ‘代表0個 a。
結合 : 當p為空,若s也為空,返回true,反之返回false; 當p的長度為1,若s長度也為1,且相同或是p為‘.‘則返回true,反之返回false;代碼如下:
1 class Solution { 2 public: 3 bool isMatch(const char *s, const char *p) 4 { 5 if(*p==‘\0‘) return *s==‘\0‘; 6 if(*(p+1) ==‘*‘) 7 { 8 while(*p==*s||(*p==‘.‘&&*s !=‘\0‘)) 9 { 10 if(isMatch(s++,p+2)) //判斷*之前元素的個數 11 return true; 12 } 13 return isMatch(s,p+2); //直接匹配字符*的下一個 14 } 15 else 16 { 17 if(*p==*s||(*p==‘.‘&&*s !=‘\0‘)) 18 return isMatch(s+1,p+1); 19 return false; 20 } 21 } 22 };
還有一種利用動態規劃的方法,暫時沒有看太懂,這裏給出鏈接1 ,鏈接2,方便以後揣摩。
[Leetcode] Regular expression matching 正則表達式匹配