-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWildcardMatching.java
More file actions
55 lines (50 loc) · 1.07 KB
/
WildcardMatching.java
File metadata and controls
55 lines (50 loc) · 1.07 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cc.dectinc.leetcode;
/**
*
*/
/**
* @author Dectinc
* @version Apr 15, 2015 11:32:10 AM
*
*/
public class WildcardMatching {
public boolean isMatch(String s, String p) {
if (s == null || p == null) {
return false;
}
int lens = s.length();
int lenp = p.length();
if (p.replace("*", "").length() > lens) {
return false;
}
boolean[] cur = new boolean[lenp + 1];
boolean[] prev = new boolean[lenp + 1];
char[] ss = s.toCharArray();
char[] pp = p.toCharArray();
prev[0] = true;
for (int i = 0; i < lenp; i++) {
if (pp[i] != '*') {
break;
}
prev[i + 1] = true;
}
for (int i = 0; i < lens; i++) {
for (int j = 0; j < lenp; j++) {
if (pp[j] == '*') {
cur[j + 1] = prev[j + 1] || cur[j];
} else {
cur[j + 1] = prev[j] && (pp[j] == '?' || pp[j] == ss[i]);
}
}
prev = cur;
cur = new boolean[lenp + 1];
}
return prev[lenp];
}
public static void main(String[] args) {
WildcardMatching sol = new WildcardMatching();
String s = "aa";
String p = "aa";
System.out.println(sol.isMatch(s, p));
}
}