-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIsSubsequence.php
53 lines (45 loc) · 1.21 KB
/
IsSubsequence.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace leetcode;
class IsSubsequence
{
public static function isSubsequence(string $s, string $t): bool
{
[$m, $n, $i, $j] = [strlen($s), strlen($t), 0, 0];
if ($m < 0 || $n < 0) {
return false;
}
while ($i < $m && $j < $n) {
if ($s[$i] === $t[$j]) {
$i++;
}
$j++;
}
return $i === $m;
}
public static function isSubsequence2(string $s, string $t): bool
{
[$m, $n] = [strlen($s), strlen($t)];
if ($m < 0 || $n < 0) {
return false;
}
$dp = array_fill(0, $m + 1, array_fill(0, $n + 1, false));
$dp[0][0] = true;
for ($i = 1; $i <= $m; $i++) {
$dp[$i][0] = false;
}
for ($j = 1; $j <= $n; $j++) {
$dp[0][$j] = true;
}
for ($i = 1; $i <= $m; $i++) {
for ($j = 1; $j <= $n; $j++) {
if ($s[$i - 1] === $t[$j - 1]) {
$dp[$i][$j] = $dp[$i - 1][$j - 1];
} else {
$dp[$i][$j] = $dp[$i][$j - 1];
}
}
}
return $dp[$m][$n];
}
}