We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4a45fbe commit 08238d5Copy full SHA for 08238d5
src/Algorithms/0139.word-break/word-break.php
@@ -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