给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
说明:
拆分时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
from typing import *
class TrieTree:
def __init__(self):
self.head = Node('#')
def add(self, word):
tem = self.head
for c in word:
if c not in tem.next:
tem.next[c] = Node(c)
tem = tem.next[c]
tem.isLeaf = True
def contains(self, word):
tem = self.head
for c in word:
if c not in tem.next:
return False
tem = tem.next[c]
return tem.isLeaf
class Node:
def __init__(self, c):
self.c = c
self.isLeaf = False
self.next = {}
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
# trie=TrieTree()
trie = set()
trie.add('')
[trie.add(word) for word in wordDict]
len1 = len(s)
dp = [False] * (len1 + 1)
dp[0] = True
for i in range(len1 + 1):
for j in range(i):
dp[i] = dp[i] or (dp[j] and s[j:i] in trie)
return dp[len1]