【leetcode】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".



题解:动态规划

设置数组dp,dp[i,j]表示从s的第i个字母(i=0~s.length-1)开始,长度为j的子字符串是否在字典dict中。则,有以下递推式:

  1. 如果字典包含子字符串s[i,i+j-1],则 dp[i,j] = true;
  2. 如果字典包含子字符串s[i,i+k-1]和字符串s[i+k,i+j],则dp[i,j] = true;
  3. 否则,dp[i,j] = false;

代码如下:

public class Solution {
    public boolean wordBreak(String s, Set<String> dict) {
        if(s == null || dict.size() == 0)
            return false;
        int length = s.length();
        boolean[][] dp = new boolean[length][length+1];

        for(int len = 1;len <= length;len++){
            for(int i = 0;i+len <= length;i++){
                String sub = s.substring(i,i+len);
                if(dict.contains(sub)){
                    dp[i][len] = true;
                    continue;
                }

                for(int k = 1;k < len;k++){
                    if(dp[i][k] && dp[i+k][len-k] )
                    {
                        dp[i][len] = true;
                        break;
                    }
                }
            }
        }

        return dp[0][length];
    }
}

对于字符串,substring这个函数返回的是beginIndex和endIndex-1之间的子字符串!

【leetcode】Word Break,布布扣,bubuko.com

时间: 2024-12-19 14:19:14

【leetcode】Word Break的相关文章

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

【leetcode】Word Break (middle)

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, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Word Break II 动态规划

题目:Word Break 要求找到所有能够有字典中的词重组成目标串的结果 public class Solution { public static List<String> wordBreak(String s, Set<String> dict) { List<String> dp[] = new ArrayList[s.length()+1]; dp[0] = new ArrayList<String>(); for(int i=0; i<s.

【LeetCode】Word Break 动态规划

题目:Word Break 思路:将一个串可以划分的共有s.length+1个点,判断长为n的串是否能由字典中的词组成,则看之前有没有划分点能使其处于字典中 ,这样该问题 就分解为子问题的求解 所以可以使用动态规划 <span style="font-size:18px;">public class Solution { public boolean wordBreak(String s, Set<String> dict) { boolean[] tag =

【leetcode】Word Break(python)

思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符開始找连续的子串,可是这时与第一个就稍有不同.比方说word='ab', dict={ 'a', ab', ...},在找到a后,接下来处理的是b.我们发现b不在dict中,可是我们发现b能够和a结合,形成ab,而ab在dict中.所以这里的每一个子串就能够有三种选择.要么自己单独作为

【leetcode】Word Break II (hard)★

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Word Search 解题报告

[题目] Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not