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
leetcode第十題,拿C++寫的時候發現初始化數組如果只寫一個值是不行的,其他元素的內存是臟的,需要寫成 {} 方能初始化為默認值。
這道題用的是動態編程,確認最優子結構即可,topdown或者 bottomup都可以。
兩個字符串 s 和 p,子問題就是是否子字符串也匹配,注意,s[0]可以不和p[0]匹配。粗略看,假設s長度為lenS,p長度為lenP,那么算法的時間復雜度應該是O(lenS*lenT) - O(n**2)。
todo:自定向下的方法,自底向上的方法,純遞歸就不寫了。
設置一個表 memo[lenS+1][lenP+1] 來記錄子字符串s[i:lenS],其中 i in range(0, lenS)。
設空字符串與空字符串默認匹配,即s[lenS:]與 p[lenP:] 是匹配的,作為哨兵。