139. Word Break

# Medium

Solution:

  1. Define a list canSegement[False]*(len(s)+1) to do DP, set canSegement[0]=True.

  2. 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.

  3. canSegment[len(s)] is the result.

Because canSegment[j] = True and {pe} is not in wordDic, canSegment[i] = False

Last updated

Was this helpful?