Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def wordBreak(self, s: str, wordDict: List[str]) -> bool:
- n = len(s)
- dp = [False]*(len(s)+1)
- dp[0] = True
- for i in range(1,n+1):
- for word in wordDict:
- if i-len(word)>=0 and s[i-len(word):i] == word and dp[i-len(word)]:
- # dp[i] - represents wordBreak ending here
- # notice dp variable running counter for n+1 variable
- # cause dp[i] - means word break possible for ith character in string
- # onlt if dp[i-len(word)] is True and i-len(word):i, notice it is enough
- # to use i-len(word):i instead of i+1 because then when we are accounting for
- # for ith iterm in dp variable we essemtially accountting for string ending at i-1
- # now to account for i-1th character we need to go back from i to i-len(word)
- dp[i] = dp[i-len(word)]
- break
- return dp[-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement