Word Break
Example
Solution
class Solution:
# @param s: A string s
# @param dict: A dictionary of words dict
def wordBreak(self, s, dict):
if len(dict) == 0:
return len(s) == 0
n = len(s)
f = [False] * (n + 1)
f[0] = True
maxLength = max([len(w) for w in dict])
for i in range(1, n + 1):
for j in range(1, min(i, maxLength) + 1):
if f[i - j] and s[i - j:i] in dict:
f[i] = True
break
return f[n]Last updated