leetcode@ [139/140] Word Break & Word Break II
https://leetcode.com/problems/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".
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
if(s.length() == ) return false;
vector<bool> canBreak(s.length(), false);
for(int i=;i<s.length();++i) {
if(wordDict.find(s.substr(, i+)) != wordDict.end()) {
canBreak[i] = true;
continue;
}
for(int pre=;pre<i;++pre) {
if(canBreak[pre] && wordDict.find(s.substr(pre+, i-pre)) != wordDict.end()) {
canBreak[i] = true;
break;
}
}
}
return canBreak[s.length()-];
}
};
https://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, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].
A solution is ["cats and dog", "cat sand dog"].
class Solution {
public:
void findBreakPoint(vector<bool>& canBreak, string& s, unordered_set<string>& wordDict) {
for(int i=;i<s.length();++i) {
if(wordDict.find(s.substr(, i+)) != wordDict.end()) {
canBreak[i] = true;
continue;
}
for(int pre=;pre<i;++pre) {
if(canBreak[pre] && wordDict.find(s.substr(pre+, i-pre)) != wordDict.end()) {
canBreak[i] = true;
break;
}
}
}
}
void dfs(vector<string>& res, vector<string>& load, vector<bool>& canBreak, string& s, unordered_set<string>& wordDict, int idx) {
if(idx == s.length()-) {
string tmp = "";
for(int i=;i<load.size()-;++i) tmp += load[i] + " ";
if(load.size()>) tmp+=load[load.size()-];
res.push_back(tmp);
return;
}
for(int nx=idx+;nx<s.length();++nx) {
if(canBreak[nx] && wordDict.find(s.substr(idx+, nx-idx)) != wordDict.end()) {
load.push_back(s.substr(idx+, nx-idx));
dfs(res, load, canBreak, s, wordDict, nx);
load.pop_back();
}
}
}
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<bool> canBreak(s.length(), false);
vector<string> res; res.clear();
vector<string> load; load.clear();
findBreakPoint(canBreak, s, wordDict);
if(canBreak[s.length()-]) dfs(res, load, canBreak, s, wordDict, -);
return res;
}
};
leetcode@ [139/140] Word Break & Word Break II的更多相关文章
- leetcode 139 单词拆分(word break)
一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- (*medium)LeetCode 211.Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- LeetCode 5:Given an input string, reverse the string word by word.
problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
随机推荐
- Unity3D开发(一):NGUI之UIRoot屏幕分辨率自适应
原地址:http://blog.csdn.net/onerain88/article/details/11713299 NGUI在Unity3D游戏开发中非常常用,而NGUI对于每一个UI场景,都是以 ...
- 区分JS中的undefined,null,"",0和false
在程序语言中定义的各种各样的数据类型中,我们都会为其定义一个"空值"或"假值",比如对象类型的空值null,.NET Framework中数据库 字段的空值DB ...
- 接口和JAVA设计模式
- mac 升级后 web 服务器起不来 问题
4. Apache 要本地调试代码的时候发现Apache也不能幸免地跪了.执行apachectl -v发现Apache已经更新到2.4.9版本了.一定又是因为版本更新出了什么差错. 4.1 修改htt ...
- QString和char字符数组之间的转换(QTextCodec.toUnicode方法,特别注意\0的问题)
How can I convert a QString to char* and vice versa ?(trolltech) Answer:In order to convert a QStrin ...
- Move to Another Changelist
Move to Another Changelist 将选中的文件转移到其他的 Change list 中. Change list 是一个重要的概念,这里需要进行重点说明.很多时候,我们开发一个项目 ...
- 列出man手册所有函数的方法
locate /man7/|sed -r 's#.*/([^/]+).7.gz$#\1#' locate /man7/ | xargs basename -a -s '.7.gz' apropos - ...
- 中断服务程序不用interrupt关键字也可实现中断,该关键字是否必须?
2013-06-20 11:13:35 中断服务程序不用interrupt关键字也可实现中断,该关键字是否必须? 使用tools->pin connect,将INT5与pin.txt关联,模拟外 ...
- PHP+Mysql无限分类的方法汇总
无限分类是个老话题了,来看看PHP结合Mysql如何实现.第一种方法这种方法是很常见.很传统的一种,先看表结构表:categoryid int 主键,自增name varchar 分类名称pid in ...
- C#中保留2位小数
public static void Method() { double a = 1.991; a = Math.Round(a); Console.WriteLine("a = {0}&q ...