给你一个字符串 s
和一个字符串列表 wordDict
作为字典。请你判断是否可以利用字典中出现的单词拼接出 s
。
注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。
示例 1:
输入: s = "leetcode", wordDict = ["leet", "code"] 输出: true 解释: 返回 true 因为 "leetcode" 可以由 "leet" 和 "code" 拼接成。
示例 2:
输入: s = "applepenapple", wordDict = ["apple", "pen"] 输出: true 解释: 返回 true 因为"
applepenapple"
可以由"
apple" "pen" "apple" 拼接成
。 注意,你可以重复使用字典中的单词。
示例 3:
输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] 输出: false
提示:
1 <= s.length <= 300
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 20
s
和wordDict[i]
仅有小写英文字母组成wordDict
中的所有字符串 互不相同
方法一:动态规划
时间复杂度
方法二:前缀树 + 记忆化搜索
根据
若存在满足条件的拆分方案,返回
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
words = set(wordDict)
n = len(s)
dp = [False] * (n + 1)
dp[0] = True
for i in range(1, n + 1):
for j in range(i):
if dp[j] and s[j:i] in words:
dp[i] = True
break
return dp[-1]
class Trie:
def __init__(self):
self.children = [None] * 26
self.is_end = False
def insert(self, w):
node = self
for c in w:
idx = ord(c) - ord('a')
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.is_end = True
def search(self, w):
node = self
for c in w:
idx = ord(c) - ord('a')
if node.children[idx] is None:
return False
node = node.children[idx]
return node.is_end
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@cache
def dfs(s):
return not s or any(trie.search(s[:i]) and dfs(s[i:]) for i in range(1, len(s) + 1))
trie = Trie()
for w in wordDict:
trie.insert(w)
return dfs(s)
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> words = new HashSet<>(wordDict);
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
if (dp[j] && words.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
}
class Trie {
Trie[] children = new Trie[26];
boolean isEnd;
void insert(String w) {
Trie node = this;
for (char c : w.toCharArray()) {
c -= 'a';
if (node.children[c] == null) {
node.children[c] = new Trie();
}
node = node.children[c];
}
node.isEnd = true;
}
boolean search(String w) {
Trie node = this;
for (char c : w.toCharArray()) {
c -= 'a';
if (node.children[c] == null) {
return false;
}
node = node.children[c];
}
return node.isEnd;
}
}
class Solution {
private Trie trie = new Trie();
private Map<String, Boolean> memo = new HashMap<>();
public boolean wordBreak(String s, List<String> wordDict) {
for (String w : wordDict) {
trie.insert(w);
}
return dfs(s);
}
private boolean dfs(String s) {
if (memo.containsKey(s)) {
return memo.get(s);
}
if ("".equals(s)) {
return true;
}
for (int i = 1; i <= s.length(); ++i) {
if (trie.search(s.substring(0, i)) && dfs(s.substring(i))) {
memo.put(s, true);
return true;
}
}
memo.put(s, false);
return false;
}
}
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> words(wordDict.begin(), wordDict.end());
int n = s.size();
vector<bool> dp(n + 1);
dp[0] = true;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
if (dp[j] && words.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
};
class Trie {
private:
vector<Trie*> children;
bool isEnd;
public:
Trie() : children(26), isEnd(false) {}
void insert(string word) {
Trie* node = this;
for (char c : word)
{
c -= 'a';
if (!node->children[c]) node->children[c] = new Trie();
node = node->children[c];
}
node->isEnd = true;
}
bool search(string word) {
Trie* node = this;
for (char c : word)
{
c -= 'a';
if (!node->children[c]) return false;
node = node->children[c];
}
return node->isEnd;
}
};
class Solution {
public:
Trie* trie = new Trie();
unordered_map<string, bool> memo;
bool wordBreak(string s, vector<string>& wordDict) {
for (auto w : wordDict) trie->insert(w);
return dfs(s);
}
bool dfs(string s) {
if (memo.count(s)) return memo[s];
if (s == "") return true;
for (int i = 1; i <= s.size(); ++i)
{
if (trie->search(s.substr(0, i)) && dfs(s.substr(i)))
{
memo[s] = true;
return true;
}
}
memo[s] = false;
return false;
}
};
func wordBreak(s string, wordDict []string) bool {
words := make(map[string]bool)
for _, word := range wordDict {
words[word] = true
}
n := len(s)
dp := make([]bool, n+1)
dp[0] = true
for i := 1; i <= n; i++ {
for j := 0; j < i; j++ {
if dp[j] && words[s[j:i]] {
dp[i] = true
break
}
}
}
return dp[n]
}
type Trie struct {
children [26]*Trie
isEnd bool
}
func newTrie() *Trie {
return &Trie{}
}
func (this *Trie) insert(word string) {
node := this
for _, c := range word {
c -= 'a'
if node.children[c] == nil {
node.children[c] = newTrie()
}
node = node.children[c]
}
node.isEnd = true
}
func (this *Trie) search(word string) bool {
node := this
for _, c := range word {
c -= 'a'
node = node.children[c]
if !node.isEnd {
return false
}
}
return true
}
public class Solution {
public bool WordBreak(string s, IList<string> wordDict) {
var words = new HashSet<string>(wordDict);
int n = s.Length;
var dp = new bool[n + 1];
dp[0] = true;
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < i; ++j)
{
if (dp[j] && words.Contains(s.Substring(j, i - j)))
{
dp[i] = true;
break;
}
}
}
return dp[n];
}
}