File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -57,3 +57,35 @@ const wordBreak = function (s, wordDict) {
57
57
const res = dfs ( 0 )
58
58
return res . filter ( ( a ) => a . join ( '' ) === s ) . map ( ( a ) => a . join ( ' ' ) )
59
59
}
60
+
61
+ // another
62
+
63
+ /**
64
+ * @param {string } s
65
+ * @param {string[] } wordDict
66
+ * @return {string[] }
67
+ */
68
+
69
+ const wordBreak = ( s , wordDict ) => {
70
+ const set = new Set ( wordDict )
71
+ return helper ( s , 0 , set )
72
+ }
73
+
74
+ function helper ( s , idx , dict ) {
75
+ if ( idx === s . length ) return [ ]
76
+ const res = [ ]
77
+ for ( let i = idx ; i < s . length ; i ++ ) {
78
+ const tmp = s . slice ( idx , i + 1 )
79
+ if ( dict . has ( tmp ) ) {
80
+ const arr = helper ( s , i + 1 , dict )
81
+ if ( i + 1 >= s . length ) {
82
+ res . push ( tmp )
83
+ } else if ( arr . length ) {
84
+ for ( let e of arr ) {
85
+ res . push ( tmp + ' ' + e )
86
+ }
87
+ }
88
+ }
89
+ }
90
+ return res
91
+ }
You can’t perform that action at this time.
0 commit comments