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"

给一个字符串s和一个字典dict,编程实现s能否由dict中组合而成。

方案一的DFS提交TLE,方案二使用动态规划

class Solution{
private:
void helper(string s,unordered_set<string>& wordDict,int start,bool& res){
if(start == int(s.length())){
res = true;
return;
}
for(int i = start;i<int(s.size());i++){
string word = s.substr(start,i - start + );
if(wordDict.find(word) != wordDict.end() && !res){ helper(s,wordDict,i+,res);
}
}
} public:
bool wordBreak(string s,unordered_set<string>& wordDict){
bool res = false;
helper(s,wordDict,,res);
return res;
}
}; class Solution2{
public:
bool wordBreak(string s,unordered_set<string>& wordDict){
vector<bool> res(s.size()+,false);
res[] = true; for(int i=;i<int(s.size())+;i++){
for(int j=;j<i;j++){
if(res[j] && wordDict.find(s.substr(j,i-j)) != wordDict.end()){
res[i] = true;
break;
}
}
}
return res.back();
}
};

Word Break的更多相关文章

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

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

  2. 【leetcode】Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  3. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  4. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  5. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  6. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  7. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  8. 【leetcode】Word Break (middle)

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

  9. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  10. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

随机推荐

  1. EF6 CodeFirst 实践系列文章列表

    2015 Jul.16 EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(一) 来自:wangweimutou 本系列源自对EF6 CodeFirst的探索 ...

  2. Thymeleaf+Spring整合

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  3. solr全文检索原理及solr5.5.0 Windows部署

    文章原理链接:http://blog.csdn.net/xiaoyu411502/article/details/44803859 自己稍微总结:全文检索主要有两个过程:创建索引,搜索索引 创建索引: ...

  4. Python笔记-集合,拷贝

    对于列表,元组,集合的异同,有如下解释 #list ----> 允许重复的集合,修改# tuple ----> 允许重复的集合,不修改# set ----> 不允许重复的集合下面是示 ...

  5. PL/SQL Developer记住用户名密码

    在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码: 设置方法:PL/SQL Developer ->tools-> ...

  6. Java特性-HashMap

    想分享一个对HashMap的理解: 我们首先要知道一个HashMap对象的构成,一般的理解是:一个Map里面放了很多个键值对,合在一起就是一个键值对的数组: 大概这么理解没问题,可是有一点要说明一下, ...

  7. python 中的高级函数sorted()

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  8. 64位Win7 VS调试、PLSQL与oracle的连接异常问题

    系统换为64位Win7后,VS与Oracle开发环境出现了很多问题.调试无法连接Oracle,PLSQL无法连接Oracle等一系列问题.下面记录一下处理办法: 1.oracle客户端选择32位进行安 ...

  9. 2.4G/5G频段WLAN的模式、带宽、协商速率

    2.4G频段 5G频段

  10. web开发中常用的技术体系

    HTML html(HyperText  Markup  Language)超文本标记语言"超文本"就是指页面内可以包含图片.链接.程序等非文字元素. 超文本标记语言的结构包括&q ...