LeetCode - Algorithms - 44. Wildcard Matching

Only mark. It’s an example of dynamic programming.
I don’t know how to solve it, what’s more, I can’t understand the answer of others, so I run code of others on leetcode.

Java

by XN W

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
class Solution {
public boolean isMatch(String s, String p) {
int pl = 0;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) != '*') {
pl++;
}
}
if (pl > s.length()) {
return false;
}
if (s.length() == 0 && pl == 0) {
return true;
}
boolean[][] store = new boolean[2][s.length() + 1];
store[0][0] = true;
store[1][0] = false;
for (int ps = 1; ps <= p.length(); ps++) {
if (p.charAt(ps - 1) == '*') {
store[1][0] = store[0][0];
} else {
store[1][0] = false;
}
for (int ss = 1; ss <= s.length(); ss++) {
if (p.charAt(ps - 1) == '?'
|| p.charAt(ps - 1) == s.charAt(ss - 1)) {
store[1][ss] = store[0][ss - 1];
} else if (p.charAt(ps - 1) != '*') {
store[1][ss] = false;
} else {
store[1][ss] = store[0][ss - 1] || store[1][ss - 1]
|| store[0][ss];
}
}
for (int i = 0; i <= s.length(); i++) {
store[0][i] = store[1][i];
}
}
return store[1][s.length()];
}
}

Submission Detail

by XN W

  • 1808 / 1808 test cases passed.
  • Runtime: 7 ms, faster than 100.00% of Java online submissions for Wildcard Matching.
  • Memory Usage: 37.7 MB, less than 93.46% of Java online submissions for Wildcard Matching.

ref

Wildcard Matching

I hate this algorithms!! Seriously!!!

  1. recursive way
  2. DP way