Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def wordBreak(self, s: str, words: List[str]) -> bool:
- n=len(s)
- dp={}
- def dfs(i):
- if i>=n:return True
- if i in dp:return dp[i]
- ans=False
- for word in words:
- if len(word)>n-i+1:continue
- w=len(word)
- if s[i:i+w]==word:
- ans=ans or dfs(i+w)
- if ans:break
- dp[i]=ans
- return dp[i]
- return dfs(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement