139 Word Break

139 Word Break

纯dp

class Solution:
    # @param s, a string
    # @param dict, a set of string
    # @return a boolean
    def wordBreak(self, s, dict):
        length = len(s)
        dp = [False]*(length + 1)
        dp[0] = True
        i = 1
        while i < length+1:
            j = i-1
            while j >= 0:
                cur = s[j:i]
                if dp[j] and cur in dict:
                    dp[i] = True
                    break
                j -= 1
            i += 1
        return dp[length]

 

posted @ 2015-07-22 04:56  dapanshe  阅读(87)  评论(0编辑  收藏  举报