-
Notifications
You must be signed in to change notification settings - Fork 4
/
10_isMatch.java
67 lines (59 loc) · 2.56 KB
/
10_isMatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class Solution {
public boolean isMatch(String s, String p) {
if(p == null) return s == null;
if(p.length() == 0) return s.length() == 0;
if(p.length() == 1){
if(p.equals("*")) return false;
if(p.equals(".")) return s.length() == 1;
return p.equals(s);
}
//p.length()>=2
if(p.charAt(1) != '*'){
if(s.length() == 0) return false;
if(p.charAt(0) == '.' || p.charAt(0) == s.charAt(0)) return isMatch(s.substring(1), p.substring(1));
return false;
} else{
if(isMatch(s,p.substring(2))) return true;
int i = 0;
while(i< s.length() && (s.charAt(i) == p.charAt(0) || p.charAt(0) == '.')){
if(isMatch(s.substring(i+1), p.substring(2))) return true;
i++;
}
}
return false;
}
}
//Reference: https://discuss.leetcode.com/topic/40371/easy-dp-java-solution-with-detailed-explanation
// 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(i-1) == s.charAt(i) or p.charAt(i-1) == '.':
// dp[i][j] = dp[i-1][j] //in this case, a* counts as multiple a
// 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
public class Solution {
public boolean isMatch(String s, String p) {
if(p == null || s == null) return false;
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[m+1][n+1];
dp[0][0] = true;
for(int j = 0; j < n; j++){
if(p.charAt(j) == '*' && dp[0][j-1]) dp[0][j+1] = dp[0][j-1];
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') dp[i+1][j+1] = dp[i][j];
if(p.charAt(j) == '*'){
if(s.charAt(i)!=p.charAt(j-1) && p.charAt(j-1)!='.') dp[i+1][j+1] = dp[i+1][j-1];
else{
dp[i+1][j+1] = (dp[i][j+1] || dp[i+1][j] || dp[i+1][j-1]);
}
}
}
}
return dp[m][n];
}
}