Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions javascript/0140-word-break-ii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Backtracking | Recursion
* Time O(n^k) | Space O(n) | n = number of words in dict, k is length of the string.
* https://leetcode.com/problems/word-break-ii
* @param {string} s
* @param {string[]} wordDict
* @return {string[]}
*/
var wordBreak = function(s, wordDict) {

wordDict = new Set(wordDict);

const allSentences = [];

const dfs = (startIdx, sentence) => {
if (startIdx >= s.length) {
if (sentence.length) {
allSentences.push(sentence.slice());
}
return;
}

for (let i = startIdx; i < s.length; i++) {
const currWord = s.slice(startIdx, i+1);
if (wordDict.has(currWord)) {
sentence.push(currWord);
dfs(i+1, sentence);
sentence.pop();
}
}
}

dfs(0, []);
for (let i = 0; i < allSentences.length; i++) {
allSentences[i] = allSentences[i].join(" ");
}
return allSentences;
};