[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)。
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的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [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 ...
- [LeetCode] Word Frequency 单词频率
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- EF – 1.模式
3种数据库 code first model first database first 创建EF http://www.cnblogs.com/tangge/p/3834578.htm ...
- POJ1699 HDU 1560 Best Sequence(AC自动机 最短路)
曾写过迭代加深搜索的方法,现在使用在AC自动上跑最短路的方法 dp[i][j]表示状态为到节点i,模式串是否包含的状态为j的最短串的长度,则状态转移方程为: dp[nx][ny] = min(dp[x ...
- 针对较大基数的排列组合算法Java实现类(n选m)
package com.utils; import java.math.BigDecimal; import java.math.RoundingMode; public class PLZUUtil ...
- .NET NLog 详解 (三) - LayoutRender
这期将NLog Git版本指向2005-06-09,NLog v0.9 released.这个时候的代码结构升级为这样: 和上期的版本相比,最明显的莫过于原先的Appender全套更名为Target. ...
- Windows环境下Oracle数据库的自动备份脚本
批处理文件(.bat) @echo off echo ================================================ echo Windows环境下Oracle数据 ...
- Oracle 备份与恢复介绍
一.Oracle备份方式分类:Oracle有两类备份方式:(1)物理备份:是将实际组成数据库的操作系统文件从一处拷贝到另一处的备份过程,通常是从磁盘到磁带.物理备份又分为冷备份.热备份: (2)逻 ...
- Win10 安装Vs2015 社区版和企业版各个问题汇总
1.前提下已经下载ISO文件 2.你的电脑没有连接网络或者你使用了宽带通类似的运营商网络. 3.你确保你正确安装了win10 并且已经激活 出现的问题如下 一.当你安装离线下载的ISO for Vs2 ...
- PHP_Memcache函数详解
memcache函数所有的方法列表如下: Memcache::add – 添加一个值,如果已经存在,则返回false Memcache::addServer – 添加一个可供使用的服务器地址 Memc ...
- PMP 第四章 项目整合管理
1.什么是整合管理,整合什么?如何整合? 项目整合管理包括识别 定义 组合 统一与协调项目管理过组的个过程及项目管理活动二进行的各种过程和活动. 整合兼具统一 合并 连接和一体化的性质,对 ...
- eclipse文本编码格式修改为UTF-8 (转)
如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简 ...