Advertisement
Iam_Sandeep

Word Break Problem

May 7th, 2022
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. class Solution:
  2.     def wordBreak(self, s: str, words: List[str]) -> bool:
  3.         n=len(s)
  4.         dp={}
  5.         def dfs(i):
  6.             if i>=n:return True
  7.             if i in dp:return dp[i]
  8.             ans=False
  9.             for word in words:
  10.                 if len(word)>n-i+1:continue
  11.                 w=len(word)
  12.                 if s[i:i+w]==word:
  13.                     ans=ans or dfs(i+w)
  14.                     if ans:break
  15.             dp[i]=ans
  16.             return dp[i]
  17.            
  18.         return dfs(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement