Word Break

https://oj.leetcode.com/problems/word-break/

Given a string s and a dictionary of words dict, determine ifs 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".

 public boolean wordBreak(String s, Set<String> dict)


这题一看就是dp题,首先还是给一个暴力解外加截枝能过OJ的方法。暴力解的解法也是类似backtrace,每一个递归层进行start到end的循环,然后判断s[start..i]是否在字典内,在的话就以i + 1作为下一层递归循环的start,直到某一个递归层出现start == s.length()的情况返回true或者返回下一层递归层的结果的或解。如果没有这样的递归层,就在该递归层返回false。截枝的方式也很简单,用一个因为实际影响递归结果的只有一个start参数,那么截枝也只需要一个一维整型数组即可,数组cached[i] = 0表示这一个节点未被访问, -1表示该节点的结果是false,1表示该节点的结果是true。那么某个递归节点只需要走一次路径即可。下面给出具体代码,热呼呼的,刚写下来的。。。

    public boolean wordBreak(String s, Set<String> dict) {
        if(s == null || s.isEmpty())
            return true;
        int[] cached = new int[s.length()];
        return helper(s, dict, cached, 0);
    }
    
    public boolean helper(String s, Set<String> dict, int[] cached, int curpos){
        if(curpos == s.length() || cached[curpos] == 1)
            return true;
        else if(cached[curpos] == -1)
            return false;
        
        boolean res = false;
        for(int i = curpos; i <= s.length() && !res; i++){
            if(dict.contains(s.substring(curpos, i))){
                res |= helper(s, dict, cached, i);
            }
        }
        if(res)
            cached[curpos] = 1;
        else
            cached[curpos] = -1;
        return res;
    }

接下来就要隆重推出我们的dp解。这一题据我所知的dp思路其实不止一种。第一种dp思路来源于上面的暴力递归解。就在代码里说明吧。

    public boolean wordBreak(String s, Set<String> dict) {
        if(s == null || s.isEmpty())
            return true;
        boolean[] dp = new boolean[s.length()];
        dp[0] = true;
        for(int i = 0; i < s.length(); i++){
            if(!dp[i])//这里就是contains(s.substring(0, curpos))的变形,
                continue;
            if(dict.contains(s.substring(i)))//这里就是 curpos == s.length()的变形
                return true;
            for(int j = i + 1; j < s.length(); j++){
                if(dict.contains(s.substring(i,j)))//这个就是dict.contains(s.substring(i,j))进入下一层递归的变形
                    dp[j] = true;
            }
        }
        return false;
    }

这一种解法的推导式有点难给。因为其实f(i)确实相当于s[0..i]是否为一个合理的wordbreak。但是这必须是遍历完全部的循环才会得到最终的答案的。这其实就是模拟了递归暴力解,但是以dp的形式。其实理解完下面这个dp之后上面这个dp会更容易的理解。

这一个dp的解法和之前Palindrome Partitioning II http://blog.youkuaiyun.com/chaochen1407/article/details/43882957 是类似的。推导式如下

f(i) = f(k) && dict.contains(s.substring(k, i))  (k == 1...i - 1) 或者 dict.contains(s.substring(0, 1)) 时f(i)为真

原理和palindrome partitioning II是一样的,在于一刀将字符串切成两部分,左边是已经计算好的结果,右边是检测是否可以匹配的结果。一旦这一刀的左右两侧均为真,这个结果必然也就是真的。下面给出对应的代码:

    public boolean wordBreak(String s, Set<String> dict) {
        boolean[] dp = new boolean[s.length()];
        for(int i = 0; i < s.length(); i++){
            if(dict.contains(s.substring(0, i + 1))){
                dp[i] = true;
            }else{
                for(int j = 0; j < i; j++){
                    if(dp[j] && dict.contains(s.substring(j + 1, i + 1))){
                        dp[i] = true;
                        break;
                    }
                }
            }
        }
        return dp[s.length() - 1];
    }

2018-01-21 Updated:

更新了一下第二种dp解法的代码,显得更简洁一些。

    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] resultArr = new boolean[s.length() + 1];
        resultArr[0] = true;
        Set<String> dict = new HashSet<String>();
        for (String word : wordDict) {
            dict.add(word);
        }
        
        for (int i = 0; i <= s.length(); i++) {
            for (int j = 0; j < i && !resultArr[i]; j++) {
                resultArr[i] = dict.contains(s.substring(j, i)) && resultArr[j];
            }
        }
        
        return resultArr[s.length()];
    }


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值