Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

Here is a simple example to under stand the code in detail. 

http://www.codeskulptor.org/#user37_7mt6s7ux3U784tc.py


Again, I would definitely finish this code in a nasty way and waste a lot of precious time in my 'stupd' coding, which is not I want to do since I realize my fault a couple of days ago. Time to learn from the elite than work clumsily myself!


The idea is to use dynamic programming:

dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems. It is applicable to problems exhibiting the properties of overlapping subproblems[1] and optimal substructure.

 In general, to solve a given problem, we need to solve different parts of the problem (subproblems), then combine the solutions of the subproblems to reach an overall solution. Often when using a more naive method, many of the subproblems are generated and solved many times. The dynamic programming approach seeks to solve each subproblem only once, thus reducing the number of computations: once the solution to a given subproblem has been computed, it is stored or "memo-ized": the next time the same solution is needed, it is simply looked up. 


class Solution:
    # @param s, a string
    # @param dict, a set of string
    # @return a boolean
    def wordBreak(self, s, dict):
        
        break_rcrd = [False for dummy_index in range(len(s) + 1)]
        break_rcrd[0] = [True]
        
        for break_index in range(1, len(s)+1):
            
            # chcek if s[0:break_index] in the dict
            if s[0: break_index] in dict:
                break_rcrd[break_index] = True
                continue
            
            # chck if the break parts of s[0:break_index] in the dict
            for tmp_break_index in range(1, break_index):
                
                if break_rcrd[tmp_break_index] == False:
                    # head is not breakable
                    # no need to check the rest
                    continue
                
                if s[tmp_break_index:break_index] not in dict:
                    # the head is breakable
                    # but the rest part is not
                    # continue the next interation in for
                    continue
                
                # head is breakable and rest parts in dict
                # the words before/include break_index is breakable
                break_rcrd[break_index] = True
                break
            
        return break_rcrd[-1]
            



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值