139. Word Break
Given a string s
and a dictionary of strings wordDict
, return true
if s
can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example :
Input: s = "leetcode", wordDict = ["leet","code"] Output: true Explanation: Return true because "leetcode" can be segmented as "leet code".
class Solution:
def wordBreak(self, s: str, wd: List[str]) -> bool:
dp = [False] * (len(s)+1)
dp[len(s)] = True
for i in range(len(s)-1, -1, -1):
for w in wd :
if i + len(w) <=len(s) and s[i: i+ len(w)] == w:
dp[i] = dp[i+len(w)]
if dp[i]:
break
return dp[0]
Explaination :
Comments
Post a Comment