139. Word Break
# Medium
Last updated
Was this helpful?
# Medium
Last updated
Was this helpful?
The problem is where to cut in this string. Record from start to end, if the substring [0: i]
can do word break. The result is [0: len(s)-1]
.
Define a list canSegement[False]*(len(s)+1)
to do DP, set canSegement[0]=True
.
Two-layer for loop to fill in all elements of canSegment. Cut from [0:i]
, if substring before cut can segement and substring after cut is contained in wordDic
, then canSegment[i]=True
.
canSegment[len(s)]
is the result.