Skip to content

Commit 46e6165

Browse files
committed
Add solution #139
1 parent 87010d2 commit 46e6165

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
136|[Single Number](./0136-single-number.js)|Easy|
8888
137|[Single Number II](./0137-single-number-ii.js)|Medium|
8989
138|[Copy List with Random Pointer](./0138-copy-list-with-random-pointer.js)|Medium|
90+
139|[Word Break](./0139-word-break.js)|Medium|
9091
141|[Linked List Cycle](./0141-linked-list-cycle.js)|Easy|
9192
142|[Linked List Cycle II](./0142-linked-list-cycle-ii.js)|Medium|
9293
143|[Reorder List](./0143-reorder-list.js)|Medium|

solutions/0139-word-break.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 139. Word Break
3+
* https://leetcode.com/problems/word-break/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s and a dictionary of strings wordDict, return true if s can be segmented into
7+
* a space-separated sequence of one or more dictionary words.
8+
*
9+
* Note that the same word in the dictionary may be reused multiple times in the segmentation.
10+
*/
11+
12+
/**
13+
* @param {string} s
14+
* @param {string[]} wordDict
15+
* @return {boolean}
16+
*/
17+
var wordBreak = function(s, wordDict) {
18+
const result = [1, ...new Array(s.length + 1).fill(0)];
19+
20+
for (let i = 1; i <= s.length; i++) {
21+
for (let j = 0; j < i; j++) {
22+
if (result[j] && wordDict.includes(s.slice(j, i))) {
23+
result[i] = 1;
24+
break;
25+
}
26+
}
27+
}
28+
29+
return result[s.length];
30+
};

0 commit comments

Comments
 (0)