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 ...
随机推荐
- java基本对象Integer,String比较相等及equal案例说明
Integer i = new Integer(100); Integer i2 = new Integer(100); if(i == i2){ System.out.println("A ...
- nagios plugins之 check_http
nagios下的check_http ZT具体参数是一个比较重要的点,我带大家来看看.. //显示版本 #./check_http -V check_http v2053 (nagios-plugin ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Ubuntu环境下手动配置Hadoop1.2.1
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- jmeter 测试java协议经验总结
对java协议的良好支持,是jmeter比loadrunner优秀的地方,但是坑也不少,本文将相关点都整理下来备忘 一. 依赖的jar包 使用IDE开发jemter java协议脚本时,需要导入以下几 ...
- php中的ceil和floo以及round函数
ceil是向上进位得到一个值的函数: floor是舍掉小数位得到一个值的函数: round是用来四舍五入的函数. ceil 定义和用法: ceil() 函数向上舍入为最接近的整数. ceil(x); ...
- JSP JSP工作原理 JSP语法 JSP声明 JSP注释 JSP指令 jsp九大隐式/内置对象
1 什么是JSP 1)为什么说,Servlet是一个动态Web开发技术呢? Servlet是基于服务端的一种动态交互技术, HttpServletRequest表示客户端到服务端的 ...
- Java汉字排序(3)按笔划排序
对于包含汉字的字符串来说,排序的方式主要有两种:一种是拼音,一种是笔画. 本文就讲述如何实现按笔划排序的比较器(Comparator). 作者:Jeff 发表于:2007年12月21日 11:27 最 ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)5-----Assembly.Unload
http://www.blogcn.com/user8/flier_lu/index.html?id=2164751&run=.04005F8 CLR 产品单元经理(Unit Manager) ...
- js模仿jquery里的几个方法parent, parentUntil, children
有时工作需要, 也是接着上一章的方法, 用js模仿jquery里的几个方法parent, parentUntil, children. function parent(node){ return no ...