標籤:
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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true
題目:
Regex的匹配,‘.‘能匹配任何一個字元,‘*‘之前必須有一個字元,兩個結合起來表示之前那個字元出現0到無窮次。
解法:
一定要注意‘*‘必須結合前面的字元一起使用。
本文及代碼引用自:http://harrifeng.github.io/algo/leetcode/regular-expression-matching.html
- 首先要理解題意:
- "a"對應"a", 這種匹配不解釋了
- 任意字母對應".", 這也是正則常見
- 0到多個相同字元x,對應"x*", 比起普通正則,這個地方多出來一個首碼x. x代表的是 相同的字元中取一個,比如"aaaab"對應是"a*b"
- "*"還有一個易於疏忽的地方就是它的"貪婪性"要有一個限度.比如"aaa"對應"a*a", 代碼邏輯不能一路貪婪到底
- Regex如果期望著一個字元一個字元的匹配,是非常不現實的.而"匹配"這個問題,非 常容易轉換成"匹配了一部分",整個匹配不匹配,要看"剩下的匹配"情況.這就很好的把 一個大的問題轉換成了規模較小的問題:遞迴
- 確定了遞迴以後,使用java來實現這個問題,會遇到很多和c不一樣的地方,因為java對字元 的控制不像c語言指標那麼靈活charAt一定要確定某個位置存在才可以使用.
- 如果pattern是"x*"類型的話,那麼pattern每次要兩個兩個的減少.否則,就是一個一個 的減少. 無論怎樣減少,都要保證pattern有那麼多個.比如s.substring(n), 其中n 最大也就是s.length()
代碼如下
public static boolean isMatch(String s, String p) { if (p.length() == 0) return s.length() == 0; // length == 1 is the case that is easy to forget. // as p is subtracted 2 each time, so if original // p is odd, then finally it will face the length 1 if (p.length() == 1) return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == ‘.‘); // next char is not ‘*‘: must match current character if (p.charAt(1) != ‘*‘) { if (s.length() == 0) return false; else return (s.charAt(0) == p.charAt(0) || p.charAt(0) == ‘.‘) && isMatch(s.substring(1), p.substring(1)); }else{ // next char is * while (s.length() > 0 && (p.charAt(0) == s.charAt(0) || p.charAt(0) == ‘.‘)) { if (isMatch(s, p.substring(2))) return true; s = s.substring(1); } return isMatch(s, p.substring(2)); } }
reference: http://www.cnblogs.com/springfor/p/3893593.html
**Regular Expression Match