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)。

 public class Solution {
public List<String> wordBreak(String s, Set<String> dict) {
List<String> result=new ArrayList<String>();
if(!wordBreakPossible(s,dict)) return result;
dfs(s,dict,result,"",0);
return result;
} private void dfs(String s, Set<String> dict, List<String> result,String temp, int start) {
// TODO Auto-generated method stub
if(start==s.length())
result.add(temp);
else{
if(start!=0)
temp+=" ";
for(int i=start;i<s.length();i++){
String word=s.substring(start, i+1);
if(dict.contains(word))
dfs(s,dict,result,temp+word,i+1);
}
}
} private boolean wordBreakPossible(String s, Set<String> dict) {
// TODO Auto-generated method stub
boolean[] state=new boolean[s.length()+1];
state[0]=true;
for(int i=1;i<=s.length();i++){
for(int j=i-1;j>=0;j--){
if(state[j]&&dict.contains(s.substring(j, i))){
state[i]=true;
break;
} }
}
return state[s.length()];
}
}

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

20150221:

又忘了在dfs之前去判断这个string是否可以被这个dictionary分割:

导致出现了TLE:

Last executed input: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
 public class Solution {
public List<String> wordBreak(String s, Set<String> dict) { List<String> res=new ArrayList<String>();
if(s==null||s.length()==0||dict==null||dict.size()==0)
return res;
if(!wordBreakPossible(s,dict)) return res;
dfs(res,s,dict,"");
return res;
}
public void dfs(List<String> res,String s,Set<String> dict,String temp){
if(s.length()==0){ res.add(temp.trim());
return;
}
for(int i=1;i<=s.length();++i){
String t=s.substring(0,i);
if(dict.contains(t)){
dfs(res,s.substring(i),dict,temp+" "+t);
}else{
continue;
}
}
}
private boolean wordBreakPossible(String s, Set<String> dict) {
// TODO Auto-generated method stub
boolean[] state=new boolean[s.length()+1];
state[0]=true;
for(int i=1;i<=s.length();i++){
for(int j=i-1;j>=0;j--){
if(state[j]&&dict.contains(s.substring(j, i))){
state[i]=true;
break;
} }
}
return state[s.length()];
}
}

[Leetcode] Word BreakII的更多相关文章

  1. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  3. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  4. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  5. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  6. [LeetCode] Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. [LeetCode] Word Frequency 单词频率

    Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...

  8. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. [LeetCode] Word Break 拆分词句

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. Spring.Net的IOC入门

    1.构造器注入 namespace Spring.Net { class Program { //构造器注入 static void Main(string[] args) { IApplicatio ...

  2. 【翻译七】java-同步

    Synchronization Threads communicate primarily by sharing access to fields and the objects reference ...

  3. JVM_Bind:8080 的解决办法【131031】

    出错情况:运行 Tomcat 时报错 含义:8080 位置显示的端口被其他进程占用 解决方法: 方法1: 开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID ...

  4. Linux 6.5網卡配置

    Linux 6.5 網卡配置 路徑:/etc/sysconfig/network-scripts 1.關閉NetworkManager服務 [root@rhcsasm2 network-scripts ...

  5. C# 令某个窗体可跟着鼠标移动

    /// <summary> /// 使窗口的中的指定控件支持运行时移动 /// TODO:运行时缩放 /// </summary> public class ControlMo ...

  6. Hbuilder连接模拟器调试

    Hbuilder是一个非常好用的HTML5开发IDE,我最喜欢的功能就是连接手机调试了,连接手机调试有两种途径,一是通过USB连接真机,二是下载安装一个安卓模拟器,让Hbuilder连接到安卓模拟器, ...

  7. 第二十四篇:导出SOUI对象到LUA脚本

    LUA是一种体积小,速度快的脚本语言.脚本语言虽然性能上和C++这样的Naitive语言相比差一点,但是开发速度快,可以方便的更新代码等,近年来受到了越来越多开发者的重视. 在SOUI框架中,我把脚本 ...

  8. Computer Graphics Research Software

    Computer Graphics Research Software Helping you avoid re-inventing the wheel since 2009! Last update ...

  9. ios 时间和毫秒数转换

    01-时间和毫秒数的相互转换 //获取毫秒数的时间戳 long inter = [[NSDate date] timeIntervalSince1970]*1000; NSLog(@"%ld ...

  10. 利用canvas实现抽奖转盘---转载别人的

    功能需求 转盘要美观,转动效果流畅. 转盘上需要显示奖品图片,并且奖品是后台读取的照片和名字. 转动动画完成后要有相应提示. 获取的奖品具体算法在数据库里操作,前端只提供最后的效果展示.   知识要点 ...