139. Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
             Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);//用list的contains方法也可以,所以这句可以不要。
        int l = s.length();
        boolean[] dp = new boolean[l + 1];
        dp[0] = true;
        for(int i = 1; i <= l; i++){
            for(int j = 0; j < i; j++){
                if(dp[j] && set.contains(s.substring(j, i))){//dp[j]是为了防止从中间随便拉个字符是true就让dp[j]为true,不能存在重复使用的s字段。例子比如catsandog,dp分别为ffttfftff。对dp[9]而言,dog本来是存在的,但是dp[6]==False所以dp[9]==false
                    dp[i] = true;
                }
            }
        }
        return dp[l];
    }
}

 

 

Fucking genius

code 转自https://leetcode.com/problems/word-break/discuss/43790/Java-implementation-using-DP-in-two-ways

以下思路转自 http://www.cnblogs.com/grandyang/p/4257740.html

这道题其实还是一道经典的DP题目,也就是动态规划Dynamic Programming。博主曾经说玩子数组或者子字符串且求极值的题,基本就是DP没差了,虽然这道题没有求极值,但是玩子字符串也符合DP的状态转移的特点。把一个人的温暖转移到另一个人的胸膛... 咳咳,跑错片场了,那是爱情转移~ 强行拉回,DP解法的两大难点,定义dp数组跟找出状态转移方程,先来看dp数组的定义,这里我们就用一个一维的dp数组,其中dp[i]表示范围[0, i)内的子串是否可以拆分,注意这里dp数组的长度比s串的长度大1,是因为我们要handle空串的情况,我们初始化dp[0]为true,然后开始遍历。注意这里我们需要两个for循环来遍历,因为此时已经没有递归函数了,所以我们必须要遍历所有的子串,我们用j把[0, i)范围内的子串分为了两部分,[0, j) 和 [j, i),其中范围 [0, j) 就是dp[j],范围 [j, i) 就是s.substr(j, ix),其中dp[j]是之前的状态,我们已经算出来了,可以直接取,只需要在字典中查找s.substr(j, i-j)是否存在了,如果二者均为true,将dp[i]赋为true,并且break掉,此时就不需要再用j去分[0, i)范围了,因为[0, i)范围已经可以拆分了。最终我们返回dp数组的最后一个值,就是整个数组是否可以拆分的布尔值了

posted @ 2019-03-14 11:25  Schwifty  阅读(164)  评论(0编辑  收藏  举报