-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWildPatternMatching.java
More file actions
40 lines (32 loc) · 1019 Bytes
/
WildPatternMatching.java
File metadata and controls
40 lines (32 loc) · 1019 Bytes
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
package dp;
import java.util.Arrays;
public class WildPatternMatching {
public boolean isMatch(String s, String p) {
int[][] dp = new int[s.length()][p.length()];
for (int[] a : dp)
Arrays.fill(a, -1);
return sol(s, p, s.length() - 1, p.length() - 1, dp);
}
public boolean sol(String s, String p, int m, int n, int[][] dp) {
if (m < 0) {
for (int i = 0; i <= n; i++)
if (p.charAt(i) != '*')
return false;
return true;
}
if (n < 0)
return false;
if (dp[m][n] != -1)
return dp[m][n] == 1;
boolean ans = false;
if (s.charAt(m) == p.charAt(n) || p.charAt(n) == '?')
ans = sol(s, p, m - 1, n - 1, dp);
if (p.charAt(n) == '*')
ans = sol(s, p, m - 1, n, dp) || sol(s, p, m, n - 1, dp);
if (ans)
dp[m][n] = 1;
else
dp[m][n] = 0;
return dp[m][n] == 1;
}
}