LeetCode139: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[i]=dp[j]+s.contais(s.substring(j,i+1));

 1 public class Solution {
 2     public boolean wordBreak(String s, Set<String> wordDict) {
 3
 4         int m=s.length();
 5         boolean isSegmented[]=new boolean[m+1];
 6         isSegmented[0]=true;
 7         int start=0;
 8         for(int i=0;i<m;i++)
 9         for(int j=0;j<=i;j++){
10            isSegmented[i+1]=isSegmented[j]&&wordDict.contains(s.substring(j,i+1));
11            if(isSegmented[i+1])
12            break;
13         }
14         return isSegmented[m];
15     }
16
17 }

时间: 2024-10-15 17:49:48

LeetCode139:Word Break的相关文章

Leetcode139题Word Break的两种动态规划解法

由leetcode139题Word Break产生的对动态规划的一点思考. 题目要求是这样的: Given a string s and a dictionary of words dict,determine if s can be segmented into a space-separatedsequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet

Word Break II

https://oj.leetcode.com/problems/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

[leecode]Word Break II

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", &q

Word Break &amp;&amp; Word Break II

Word Break && Word Break II 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 = ["

[C++]LeetCode: 113 Word Break II (DP &amp;&amp; Backtacking) 求解拆分组合

题目: 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

leetcode[140] Word Break II

这题是上一题的加强版,这里需要返回的是所有可能的分割词.例如: s = "catsanddog",dict = ["cat", "cats", "and", "sand", "dog"]. A solution is ["cats and dog", "cat sand dog"]. 先用dp求得每个起点到终点是否符合word break 1中的条

Leetcode dfs Word Break II

Word Break II Total Accepted: 15138 Total Submissions: 92228My Submissions 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 ex

Leetcode dp Word Break

Word Break Total Accepted: 22281 Total Submissions: 105657My Submissions 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 = "leetcod

【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"