No.010 Regular Expression Matching
阿新 • • 發佈:2022-04-29
10. Regular Expression Matching
- Total Accepted: 89193
- Total Submissions: 395441
- Difficulty: Hard
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 思路:
參考自:http://www.tuicool.com/articles/Ebiymu
- 首先要理解題意:
- "a"對應"a", 這種匹配不解釋了
- 任意字母對應".", 這也是正則常見
- 0到多個相同字元x,對應"x*", 比起普通正則,這個地方多出來一個字首x. x代表的是 相同的字元中取一個,比如"aaaab"對應是"a*b"
- "*"還有一個易於疏忽的地方就是它的"貪婪性"要有一個限度.比如"aaa"對應"a*a", 程式碼邏輯不能一路貪婪到底
- 正則表示式如果期望著一個字元一個字元的匹配,是非常不現實的.而"匹配"這個問題,非 常容易轉換成"匹配了一部分",整個匹配不匹配,要看"剩下的匹配"情況.這就很好的把 一個大的問題轉換成了規模較小的問題:遞迴
- 確定了遞迴以後,使用java來實現這個問題,會遇到很多和c不一樣的地方,因為java對字元 的控制不像c語言指標那麼靈活charAt一定要確定某個位置存在才可以使用.
- 如果pattern是"x*"型別的話,那麼pattern每次要兩個兩個的減少.否則,就是一個一個 的減少. 無論怎樣減少,都要保證pattern有那麼多個.比如s.substring(n), 其中n 最大也就是s.length()
1 public boolean isMatch(String s, String p) { 2 // base case 3 if (p.length() == 0) { 4 return s.length() == 0; 5 } 6 7 // special case 8 if (p.length() == 1) { 9 // if the length of s is 0, return false 10 if (s.length() < 1) { 11 return false; 12 } 13 //if the first does not match, return false 14 else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) { 15 return false; 16 } 17 18 // otherwise, compare the rest of the string of s and p. 19 else { 20 return isMatch(s.substring(1), p.substring(1)); 21 } 22 } 23 24 // case 1: when the second char of p is not '*' 25 if (p.charAt(1) != '*') { 26 if (s.length() < 1) { 27 return false; 28 } 29 if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) { 30 return false; 31 } else { 32 return isMatch(s.substring(1), p.substring(1)); 33 } 34 } 35 36 // case 2: when the second char of p is '*', complex case. 37 else { 38 //case 2.1: a char & '*' can stand for 0 element 39 if (isMatch(s, p.substring(2))) { 40 return true; 41 } 42 43 //case 2.2: a char & '*' can stand for 1 or more preceding element, 44 //so try every sub string 45 int i = 0; 46 while (i<s.length() && (s.charAt(i)==p.charAt(0) || p.charAt(0)=='.')){ 47 if (isMatch(s.substring(i + 1), p.substring(2))) { 48 return true; 49 } 50 i++; 51 } 52 return false; 53 } 54 }