139 - Word Break
Written on October 21, 2015
Tweet
Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words.
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
if not wordDict:
return False
dp = [False] * (len(s) + 1)
dp[0] = True
for i in range(1, len(s) + 1):
for j in range(i):
if dp[j] and s[j: i] in wordDict:
dp[i] = True
break
return dp[-1]