-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.php
33 lines (31 loc) · 931 Bytes
/
Solution.php
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
class Solution {
/**
* @param string $s
* @param string $p
* @return boolean
*/
function isMatch($s, $p) {
$lengthS = strlen($s);
$lengthP = strlen($p);
$dp = [];
for ($i = 0; $i <= $lengthS; $i++) {
$dp[$i] = array_fill(0, $lengthP + 1, false);
}
$dp[0][0] = true;
for ($i = 1; $i <= $lengthP; $i++) {
if ($p[$i - 1] == '*') {
$dp[0][$i] = $dp[0][$i - 1];
}
}
for ($i = 1; $i <= $lengthS; $i++) {
for ($j = 1; $j <= $lengthP; $j++) {
if ($p[$j - 1] == '?' || $s[$i - 1] == $p[$j - 1]) {
$dp[$i][$j] = $dp[$i - 1][$j - 1];
} elseif ($p[$j - 1] == '*') {
$dp[$i][$j] = $dp[$i][$j - 1] || $dp[$i - 1][$j];
}
}
}
return $dp[$lengthS][$lengthP];
}
}