[leedcode 139] 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".

public class Solution {
    //DP思想,状态量dp[i],表示字符串s的前i个字符组成的子串是否能够进行单词拆分。
    //dp[0]=false;
    //如果一个字符串 s[0,i) 能被拆分,则一定能找到一个j使得:s[0,j) 能被拆分 且 s[j,i) 在字典中
    public boolean wordBreak(String s, Set<String> wordDict) {
        boolean dp[]=new boolean[s.length()+1];
        dp[0]=true;
        for(int i=1;i<=s.length();i++){
            for(int j=0;j<i;j++){
                if(dp[j]&&wordDict.contains(s.substring(j,i))){
                     dp[i]=true;
                     break;
                }

            }
        }
        return dp[s.length()];
    }
}
时间: 2024-10-10 18:11:47

[leedcode 139] Word Break的相关文章

139. Word Break(js)

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 r

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

[leedcode 140] 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"

139. Word Break java solutions

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 #139. Word Break C#

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

动态规划之139 Word Break

题目链接:https://leetcode-cn.com/problems/word-break/ 参考链接:https://blog.csdn.net/c_flybird/article/details/80703494 http://www.cnblogs.com/springfor/p/3874731.html 这种题目一般出现字符串.子数组都需要使用动态规划 dp[i],前i个字符串是否在字典中. dp[0]=true;空字符肯定是在字典中. public boolean wordBre

leetcode 139 word break (单词拆分)

一开始的错误答案与错误思路,幻想直接遍历得出答案: 1 class Solution { 2 public: 3 bool wordBreak(string s, vector<string>& wordDict) { 4 for(int i;i<s.size();i++){ 5 int step=0; 6 for(int j;j<wordDict.size();j++){ 7 if(s.substr(i,wordDict[j].size())==wordDict[j]){

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

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