139. Word Break

139. Word Break

import copy
class Solution:

    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        dp =[0 for i in range(len(s)+1)]
        words=set(wordDict)
        dp[0]=1
        for i in range(1,len(s)+1):
            for j in range(i-1,-1,-1):
                if dp[j]==0:continue
                if s[j:i] in words:
                    dp[i]=1
                    break
        return dp[len(s)]

你可能感兴趣的:(leetcode)