File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 87
87
136|[ Single Number] ( ./0136-single-number.js ) |Easy|
88
88
137|[ Single Number II] ( ./0137-single-number-ii.js ) |Medium|
89
89
138|[ Copy List with Random Pointer] ( ./0138-copy-list-with-random-pointer.js ) |Medium|
90
+ 139|[ Word Break] ( ./0139-word-break.js ) |Medium|
90
91
141|[ Linked List Cycle] ( ./0141-linked-list-cycle.js ) |Easy|
91
92
142|[ Linked List Cycle II] ( ./0142-linked-list-cycle-ii.js ) |Medium|
92
93
143|[ Reorder List] ( ./0143-reorder-list.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments