Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08238d5

Browse files
committed
Add solution 139.
1 parent 4a45fbe commit 08238d5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
3+
/**
4+
* @param String $s
5+
* @param String[] $wordDict
6+
* @return Boolean
7+
*/
8+
function wordBreak($s, $wordDict) {
9+
$len = strlen($s);
10+
$end;
11+
$a = array_fill(0, $len, false);
12+
$a[0] = true;
13+
for ($i = 0; $i < $len; $i++) {
14+
if (!$a[$i]) continue;
15+
foreach ($wordDict as $word) {
16+
$end = $i + strlen($word);
17+
if ($end <= $len && substr($s, $i, $end - $i) == $word) {
18+
if ($end == $len) return true;
19+
$a[$end] = true;
20+
}
21+
}
22+
}
23+
return false;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /