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

难度:100,情况分得太多太细,跟Wildcard Matching很像又比那个难多了,refer to: https://discuss.leetcode.com/topic/40371/easy-dp-java-solution-with-detailed-explanation

Leetcode: Regular Expression Matching

Here are some conditions to figure out, then the logic can be very straightforward.

1, If p.charAt(j) == s.charAt(i) :  dp[i][j] = dp[i-1][j-1];
2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
3, If p.charAt(j) == '*': 
   here are two sub conditions:
               1   if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2]  //in this case, a* only counts as empty
               2   if p.charAt(j-1) == s.charAt(i) or p.charAt(j-1) == '.':
                              dp[i][j] = dp[i-1][j]    //in this case, a* counts as multiple a (s.charAt(i) of course match with * since s.charAt(i)==p.charAt(j-1))
                           or dp[i][j] = dp[i][j-1]   // in this case, a* counts as single a
                           or dp[i][j] = dp[i][j-2]   // in this case, a* counts as empty

 1 public class Solution {
 2     public boolean isMatch(String s, String p) {
 3     
 4         if (s == null || p == null) {
 5             return false;
 6         }
 7         boolean[][] dp = new boolean[s.length()+1][p.length()+1];
 8         dp[0][0] = true;
 9         for (int i = 0; i < p.length(); i++) {
10             if (p.charAt(i) == '*' && dp[0][i-1]) {
11                 dp[0][i+1] = true;
12             }
13         }
14         for (int i = 0 ; i < s.length(); i++) {
15             for (int j = 0; j < p.length(); j++) {
16                 if (p.charAt(j) == '.') {
17                     dp[i+1][j+1] = dp[i][j];
18                 }
19                 if (p.charAt(j) == s.charAt(i)) {
20                     dp[i+1][j+1] = dp[i][j];
21                 }
22                 if (p.charAt(j) == '*') {
23                     if (p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.') {
24                         dp[i+1][j+1] = dp[i+1][j-1];
25                     } else {
26                         dp[i+1][j+1] = (dp[i+1][j] || dp[i][j+1] || dp[i+1][j-1]);
27                     }
28                 }
29             }
30         }
31         return dp[s.length()][p.length()];
32     }
33 }

一样的思路,我更习惯的dp方法:

 1 public class Solution {
 2     public boolean isMatch(String s, String p) {
 3     
 4         if (s == null || p == null) {
 5             return false;
 6         }
 7         boolean[][] dp = new boolean[s.length()+1][p.length()+1];
 8         dp[0][0] = true;
 9         for (int i = 1; i <= p.length(); i++) {
10             if (p.charAt(i-1) == '*' && dp[0][i-2]) {
11                 dp[0][i] = true;
12             }
13         }
14         for (int i = 1 ; i <= s.length(); i++) {
15             for (int j = 1; j <= p.length(); j++) {
16                 if (p.charAt(j-1) == '.' || p.charAt(j-1) == s.charAt(i-1)) {
17                     dp[i][j] = dp[i-1][j-1];
18                 }
19                 
20                 if (p.charAt(j-1) == '*') {
21                     if (p.charAt(j-2) != s.charAt(i-1) && p.charAt(j-2) != '.') {
22                         dp[i][j] = dp[i][j-2];
23                     } else { // p.charAt(j-2)==s.charAt(i-1) || p.charAt(j-2)=='.'
24                         dp[i][j] = (dp[i-1][j] || dp[i][j-1] || dp[i][j-2]);
25                     }
26                 }
27             }
28         }
29         return dp[s.length()][p.length()];
30     }
31 }