Skip to content

Commit 48ed34c

Browse files
committed
Add solution 97.
1 parent 34fde8a commit 48ed34c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
3+
/**
4+
* @param String $s1
5+
* @param String $s2
6+
* @param String $s3
7+
* @return Boolean
8+
*/
9+
function isInterleave($s1, $s2, $s3) {
10+
$l1 = strlen($s1); $l2 = strlen($s2); $l3 = strlen($s3);
11+
if ($l1 + $l2 != $l3) return false;
12+
$dp = array_fill(0, $l1 + 1, array_fill(0, $l2 + 1, false));
13+
$dp[0][0] = true;
14+
for ($i = 1; $i <= $l1; $i++) {
15+
if ($s1[$i - 1] == $s3[$i - 1]) $dp[$i][0] = true;
16+
else break;
17+
}
18+
19+
for ($j = 1; $j <= $l2; $j++) {
20+
if ($s2[$j - 1] == $s3[$j - 1]) $dp[0][$j] = true;
21+
else break;
22+
}
23+
for ($i = 1; $i <= $l1; $i++) {
24+
for ($j = 1; $j <= $l2; $j++) {
25+
$dp[$i][$j] = ($dp[$i - 1][$j] && $s1[$i - 1] == $s3[$i + $j - 1])
26+
|| ($dp[$i][$j - 1] && $s2[$j - 1] == $s3[$i + $j - 1]);
27+
}
28+
}
29+
return $dp[$l1][$l2];
30+
}
31+
}

0 commit comments

Comments
 (0)