Regular Expression Matching leetcode java


題目

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://harrifeng.github.io/algo/leetcode/regular-expression-matching.html
  • 首先要理解題意:
    • "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  static  boolean isMatch(String s, String p) {
 2          if (p.length() == 0)
 3              return s.length() == 0;
 4 
 5          //  length == 1 is the case that is easy to forget.
 6           //  as p is subtracted 2 each time, so if original
 7           //  p is odd, then finally it will face the length 1
 8           if (p.length() == 1)
 9              return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');
10 
11          //  next char is not '*': must match current character
12           if (p.charAt(1) != '*') {
13              if (s.length() == 0)
14                  return  false;
15              else
16                  return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
17                         && isMatch(s.substring(1), p.substring(1));
18         } else{
19              //  next char is *
20               while (s.length() > 0 && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {
21                  if (isMatch(s, p.substring(2))) 
22                      return  true;
23                 s = s.substring(1);
24             }
25              return isMatch(s, p.substring(2));
26         }
27     }
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM