-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSolution.php
35 lines (32 loc) · 983 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
34
35
<?php
class Solution {
/**
* @param String $s1
* @param String $s2
* @param String $s3
* @return Boolean
*/
function isInterleave($s1, $s2, $s3) {
$l1 = strlen($s1);
$l2 = strlen($s2);
$l3 = strlen($s3);
if ($l1 + $l2 != $l3) return false;
$dp = array_fill(0, $l1 + 1, array_fill(0, $l2 + 1, false));
$dp[0][0] = true;
for ($i = 1; $i <= $l1; $i++) {
if ($s1[$i - 1] == $s3[$i - 1]) $dp[$i][0] = true;
else break;
}
for ($j = 1; $j <= $l2; $j++) {
if ($s2[$j - 1] == $s3[$j - 1]) $dp[0][$j] = true;
else break;
}
for ($i = 1; $i <= $l1; $i++) {
for ($j = 1; $j <= $l2; $j++) {
$dp[$i][$j] = ($dp[$i - 1][$j] && $s1[$i - 1] == $s3[$i + $j - 1])
|| ($dp[$i][$j - 1] && $s2[$j - 1] == $s3[$i + $j - 1]);
}
}
return $dp[$l1][$l2];
}
}