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".

思路:DP。

维护一个一维bool类型数组DP[]。DP[i]表示字符串S从第0位到第i位子串是否能够分割成字典里的单词。

然后遍历一遍字典,记录下字典中单词的最大长度和最小长度,分别记为max_l和min_l。

之后进行二重循环,外层循环枚举的是单词在S中的结束位置,因为已知最短的单词长为min_l,设S长为n,则枚举的范围是min_l - 1到n - 1。

内层循环枚举的是单词的开始位置,即以外层循环所枚举的结束位置为基准,向前找min_l到max_l的距离,更近或更远都不可能存在单词了。

这里注意一个点,内层循环在枚举了一个开始位置后,设其为i,若i不为0,则判断下dp[i - 1]是否为true,若为false则直接continue。因为若前面都无法由单词构成,则这里就算由单词构成也没什么用。

最后dp[n - 1]即为结果。

 class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int n = s.size();
if (n == ) return false;
vector<bool> dp(n, false);
unordered_set<string>::iterator iter = wordDict.begin();
unordered_set<string>::iterator end = wordDict.end();
int max_l = INT_MIN;
int min_l = INT_MAX;
for (; iter != end; iter++)
{
int size = iter->size();
max_l = max(max_l, size);
min_l = min(min_l, size);
}
for (int i = min_l - ; i < n; i++)
for (int j = i - min_l + ; j >= && j >= i - max_l + ; j--)
{
if (j - >= && !dp[j - ]) continue;
string word = s.substr(j, i - j + );
if (wordDict.count(word) > ) dp[i] = true;
}
return dp[n - ];
}
};

Word Break - LeetCode的更多相关文章

  1. Word Break leetcode java

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

  2. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

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

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

  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】Word Break (middle)

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

  8. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  9. Word Break II leetcode java

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

随机推荐

  1. Java技术——Java多线程学习

    )适合多个相同程序代码的线程区处理同一资源的情况.比如下面这个买票的例子. //使用Thread实现 public static class MyThread extends Thread{ priv ...

  2. LyaoutParameters作用

    当你想要动态生成布局的时候,那么就要用到这个参数了.因为那时候你在布局文件里面写的width和height都不起作用了. LinearLayout linearLayout = (LinearLayo ...

  3. python中pip 出错

    错误:error in launcher: Unable to create process using '" python多个版本时出现, 解决方法-- 将pip重新安装 python3 ...

  4. 在 Amazon AWS 搭建及部署网站:序

    最近玩了把 AWS,实现了服务器的创建.PHP+MySql运行环境.代码部署等.一方面,后面的项目会反复重复这个流程,需要一份手册,另一方面,也给自己一个记录.于是把整个过程和要点整理一下,发到自己的 ...

  5. linux常用命令与系统管理常用命令

    linux命令:切换用户:开启ftp服务:service vsftpd start 开启ssh服务:service sshd start普通用户切换到超级用户:su rootlogout:(注销)un ...

  6. python 学习分享-面向对象

    好激动,学了四个月的面向对象,我终于勉勉强强的把作业做完了,现在来重构我的面向对象的知识! 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 ...

  7. python 学习分享-实战篇增删改查作业

    一大波函数来袭 作业要求: 1本次作业通过空格及逗号,将文件拆分成列表,在通过判断add.del.update.select等关键字,来判断用户执行的是哪种命令,根据不同的命令调用不同的函数去处理. ...

  8. C# 调试之 Debug.WriteLine()、Trace.WriteLine()

    Trace 类 和 Debug 类的区别在于,Trace 类会同时在 Debug.Release 模式下起作用,而 Debug 只作用在 Debug 模式下. 区别: 1. 输出跟踪信息 Trace. ...

  9. CSU-2019 Fleecing the Raffle

    CSU-2019 Fleecing the Raffle Description A tremendously exciting raffle is being held, with some tre ...

  10. 在windows64位上安装Python3.0

    1.下载安装包 下载地址:https://www.python.org/downloads/ 如果要下载帮助文件:Download Windows help file 如果要下载基于网页的安装程序: ...