File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean wordBreak (String s , List <String > wordDict ) {
3+ int n = s .length ();
4+ boolean [] dp = new boolean [n +1 ];
5+ dp [0 ] = true ;
6+
7+ Set <String > wordSet = new HashSet <>(wordDict );
8+
9+ for (int i = 1 ; i <= n ; i ++) {
10+ for (int j = 0 ; j < i ; j ++) {
11+ if (dp [j ] && wordSet .contains (s .substring (j , i ))) {
12+ dp [i ] = true ;
13+ break ;
14+ }
15+ }
16+ }
17+ return dp [n ];
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ function wordBreak ( s : string , wordDict : string [ ] ) : boolean {
2+ const n = s . length ;
3+ const dp :boolean [ ] = Array ( n + 1 ) . fill ( false ) ;
4+ dp [ 0 ] = true ;
5+
6+ const wordSet = new Set ( wordDict ) ;
7+
8+ for ( let i = 1 ; i <= n ; i ++ ) {
9+ for ( let j = 0 ; j < i ; j ++ ) {
10+ if ( dp [ j ] && wordSet . has ( s . substring ( j , i ) ) ) {
11+ dp [ i ] = true ;
12+ break ;
13+ }
14+ }
15+ }
16+ return dp [ n ] ;
17+ } ;
You can’t perform that action at this time.
0 commit comments