You are given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
Input: s = "neetcode", wordDict = ["neet","code"]
Output: ["neet code"]Example 2:
Input: s = "racecariscar", wordDict = ["racecar","race","car","is"]
Output: ["racecar is car","race car is car"]Example 3:
Input: s = "catsincars", wordDict = ["cats","cat","sin","in","car"]
Output: []Constraints:
1 <= s.length <= 201 <= wordDict.length <= 10001 <= wordDict[i].length <= 10s and wordDict[i] consist of only lowercase English letters.wordDict are unique.class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
wordDict = set(wordDict)
def backtrack(i):
if i == len(s):
res.append(" ".join(cur))
return
for j in range(i, len(s)):
w = s[i:j + 1]
if w in wordDict:
cur.append(w)
backtrack(j + 1)
cur.pop()
cur = []
res = []
backtrack(0)
return resWhere is the length of the string and is the sum of the lengths of the strings in the .
class TrieNode:
def __init__(self):
self.children = {}
self.isWord = False
class Trie:
def __init__(self):
self.root = TrieNode()
def addWord(self, word):
curr = self.root
for c in word:
if c not in curr.children:
curr.children[c] = TrieNode()
curr = curr.children[c]
curr.isWord = True
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
trie = Trie()
for word in wordDict:
trie.addWord(word)
def backtrack(i, path):
if i == len(s):
res.append(" ".join(path))
return
node = trie.root
word = []
for i in range(i, len(s)):
char = s[i]
if char not in node.children:
break
word.append(char)
node = node.children[char]
if node.isWord:
path.append("".join(word))
backtrack(i + 1, path)
path.pop()
res = []
backtrack(0, [])
return resWhere is the length of the string and is the sum of the lengths of the strings in the .
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
wordDict = set(wordDict)
cache = {}
def backtrack(i):
if i == len(s):
return [""]
if i in cache:
return cache[i]
res = []
for j in range(i, len(s)):
w = s[i:j + 1]
if w not in wordDict:
continue
strings = backtrack(j + 1)
for substr in strings:
sentence = w
if substr:
sentence += " " + substr
res.append(sentence)
cache[i] = res
return res
return backtrack(0)Where is the length of the string and is the sum of the lengths of the strings in the .
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
wordSet = set(wordDict)
n = len(s)
dp = [[] for _ in range(n + 1)]
dp[0] = [""]
for i in range(1, n + 1):
for j in range(i):
if s[j:i] in wordSet:
for sentence in dp[j]:
dp[i].append((sentence + " " + s[j:i]).strip())
return dp[n]Where is the length of the string and is the sum of the lengths of the strings in the .
class TrieNode:
def __init__(self):
self.children = {}
self.isWord = False
class Trie:
def __init__(self):
self.root = TrieNode()
def addWord(self, word):
curr = self.root
for c in word:
if c not in curr.children:
curr.children[c] = TrieNode()
curr = curr.children[c]
curr.isWord = True
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
trie = Trie()
for word in wordDict:
trie.addWord(word)
cache = {}
def backtrack(index):
if index == len(s):
return [""]
if index in cache:
return cache[index]
res = []
curr = trie.root
for i in range(index, len(s)):
char = s[i]
if char not in curr.children:
break
curr = curr.children[char]
if curr.isWord:
for suffix in backtrack(i + 1):
if suffix:
res.append(s[index:i + 1] + " " + suffix)
else:
res.append(s[index:i + 1])
cache[index] = res
return res
return backtrack(0)Where is the length of the string and is the sum of the lengths of the strings in the .