Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. class Solution:
  2. def wordBreak(self, s: str, wordDict: List[str]) -> bool:
  3. res = [False]
  4. mapp = dict()
  5. self.recursive(s, set(wordDict), 0, res, mapp)
  6. return res[0]
  7.  
  8.  
  9.  
  10.  
  11. def recursive(self, s, words, i, res, mapp):
  12. if i >= len(s):
  13. return False
  14.  
  15. if i in mapp:
  16. return mapp[i]
  17.  
  18. if s[i: len(s)] in words:
  19. res[0] = True
  20. return True
  21.  
  22. j = i
  23. while j < len(s):
  24. if s[i:j+1] in words:
  25. result = self.recursive(s, words, j+1, res, mapp)
  26. mapp[j+1] = result
  27. if result:
  28. return result
  29. j += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement