【发布时间】:2020-04-12 15:53:52
【问题描述】:
以下代码的时间复杂度分析和空间复杂度分析是什么:
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
if not s or not dict:
False
N=len(s)
ans=[False for i in range (N+1)]
ans[0]=True
for index in range(N):
if ans[index]:
for word in wordDict:
L=len(word)
if index+L <= N and s[index:index+L]==word:
ans[index+L]=True
return ans[-1]
给定一个非空字符串s 和一个包含非空单词列表的字典wordDict,确定s 是否可以分割成一个或多个字典单词的空格分隔序列。
【问题讨论】:
-
O(N * |wordDict|)
标签: python time-complexity big-o