-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_177wildCard.java
More file actions
35 lines (35 loc) · 1.01 KB
/
_177wildCard.java
File metadata and controls
35 lines (35 loc) · 1.01 KB
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
public class _177wildCard {
public static boolean isMatch(String s, String p){
int n = s.length();
int m = p.length();
boolean dp[][] = new boolean[n+1][m+1];
// initialize
dp[0][0]=true;
//pattern = ""
for(int i=1;i<n+1;i++){
dp[i][0]=false;
}
for(int j =1;j<m+1;j++){
if(p.charAt(j-1)=='*'){
dp[0][j]=dp[0][j-1];
}
}
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if(s.charAt(i-1)==p.charAt(j-1)|| p.charAt(j-1)=='?'){
dp[i][j]=dp[i-1][j-1];
} else if(p.charAt(j-1)=='*'){
dp[i][j]=dp[i-1][j]||dp[i][j-1];
} else{
dp[i][j]=false;
}
}
}
return dp[n][m];
}
public static void main(String[] args) {
String s = "baaabab";
String p = "*****ba*****ab";
System.out.println(isMatch(s, p));
}
}