-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDecodeWays.php
58 lines (49 loc) · 1.46 KB
/
DecodeWays.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
54
55
56
57
58
<?php
declare(strict_types=1);
namespace leetcode;
class DecodeWays
{
public static function numDecodings(string $s): int
{
$n = strlen($s);
if ($n <= 0 || (isset($s[0]) && $s[0] === '0')) {
return 0;
}
$dp = array_fill(0, $n + 1, 0);
[$dp[0], $dp[1]] = [1, 1];
for ($i = 2; $i <= $n; $i++) {
[$p, $q] = [(int) $s[$i - 1], (int) $s[$i - 2]];
$dp[$i] = $p === 0 ? 0 : $dp[$i - 1];
if ($q === 1 || ($q === 2 && $p <= 6)) {
$dp[$i] += $dp[$i - 2];
}
}
return $dp[$n];
}
public static function numDecodings2(string $s): int
{
$n = strlen($s);
if ($n <= 0 || (isset($s[0]) && $s[0] === '0')) {
return 0;
}
$helper = static function ($s, $start, $length) {
$len = $start > $length ? $start : $length;
$pos = $start > $length ? 1 : $start;
$tmp = substr($s, 0, $len);
return substr($tmp, $pos, $len);
};
$dp = array_fill(0, $n + 1, 0);
[$dp[0], $dp[1]] = [1, 1];
for ($i = 2; $i <= $n; $i++) {
$p = $helper($s, $i - 1, $i);
$q = $helper($s, $i - 2, $i);
if ($p >= 1 && $p <= 9) {
$dp[$i] += $dp[$i - 1];
}
if ($q >= 10 && $q <= 26) {
$dp[$i] += $dp[$i - 2];
}
}
return $dp[$n];
}
}