[Leetcode] Word BreakII

Question:

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", "and", "sand", "dog"]
.

A solution is ["cats and dog", "cat sand dog"].

----------------------------------------------

Solution:

dfs.

但是需要注意的是,在dfs之前,需要判断这个string能不能被这个dictionary分割(Word
Break
)。


 1 public class Solution {
2 public List<String> wordBreak(String s, Set<String> dict) {
3 List<String> result=new ArrayList<String>();
4 if(!wordBreakPossible(s,dict)) return result;
5 dfs(s,dict,result,"",0);
6 return result;
7 }
8
9 private void dfs(String s, Set<String> dict, List<String> result,String temp, int start) {
10 // TODO Auto-generated method stub
11 if(start==s.length())
12 result.add(temp);
13 else{
14 if(start!=0)
15 temp+=" ";
16 for(int i=start;i<s.length();i++){
17 String word=s.substring(start, i+1);
18 if(dict.contains(word))
19 dfs(s,dict,result,temp+word,i+1);
20 }
21 }
22 }
23
24 private boolean wordBreakPossible(String s, Set<String> dict) {
25 // TODO Auto-generated method stub
26 boolean[] state=new boolean[s.length()+1];
27 state[0]=true;
28 for(int i=1;i<=s.length();i++){
29 for(int j=i-1;j>=0;j--){
30 if(state[j]&&dict.contains(s.substring(j, i))){
31 state[i]=true;
32 break;
33 }
34
35 }
36 }
37 return state[s.length()];
38 }
39 }

时间: 2024-10-11 13:14:30

[Leetcode] Word BreakII的相关文章

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

[LeetCode] Word Search [37]

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

LeetCode: Word Search [079]

[题目] 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

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: Word Break [139]

[题目] 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 be

LeetCode: Word Break II [140]

[题目] 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", "cat

[leetcode]Word Break II @ Python

原题地址: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 = "c

LeetCode &quot;Word Ladder&quot; - TRICKY

It is not as easy as I thought it to be, mostly because of time\space limitation. And actually that's the punch line of this problem My intuition was DFS. I got several failing attemption - all TLE or MLE. Since what we are supposed to find is 'short

LeetCode: Word Break I &amp;&amp; II

I title: 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