Question

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

Solution

用动态规划求解,遍历所有长度,看当前长度i是否可以完全分化。 长度为i的字符串分为两部分,前一部分是已经算过的是否可以划分的,只需要在字典中找后面一部分是否存在。

时间复杂度为O(n^2),空间复杂度为O(n).

Code

class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
vector<bool> dp(s.length() + 1, false);
dp[0] = true; for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j <= i; j++) {
if (dp[j]) {
string str = s.substr(j, i - j);
if (dict.find(str) != dict.end()) {
dp[i] = true;
break;
}
}
}
}
return dp[s.length()];
}
};

LeetCode——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(DP)

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

  3. LeetCode Word Break II

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

  4. [leetcode]Word Break II @ Python

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

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

  6. LeetCode ||& Word Break && Word Break II(转)——动态规划

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

  7. [LeetCode] Word Break II 解题思路

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

  8. [Leetcode] word break ii拆分词语

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

  9. LeetCode: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

  10. [LeetCode] Word Break 拆分词句

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

随机推荐

  1. 《转》适用于开发人员的10个最佳ASP.NET的CMS系统

    1) mojoportal mojoPortal 是一个开源的.用 C# 编写的站点框架和内容管理系统,可以运行在 Windows 中的 ASP.NET 和 Linux/Mac OS X 中的 Mon ...

  2. C语言函数重入

    C语言函数重入 可重入函数:可以被中断的函数,即这个函数执行时,可以中断其执行,可以由一个或多个任务并发使用,而不比担心数据错误. 不可重入函数(不安全函数) 不能运行在多任务环境下,除非能保证互斥( ...

  3. wireshark抓取OpenFlow数据包

    在写SDN控制器应用或者改写控制器源码的时候,经常需要抓包,验证网络功能,以及流表的执行结果等等,wireshark是个很好的抓包分析包的网络工具,下面简介如何用wireshark软件抓取OpenFl ...

  4. c语言加密算法头文件下载(base64、md5、sha1)

    1.base64 网上有一份开头就是 // Encoding lookup table char base64encode_lut[] = {  的base64.h, 在loadrunner中加密有b ...

  5. Codeforces Round #372 (Div. 1) B. Complete The Graph

    题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...

  6. duboo服务调用不到的原因(dubbo启动消费者报错:No provider available for the service)

    com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method queryTemplate in the service com.x.a ...

  7. [HackerRank] The Longest Common Subsequence

    This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...

  8. JavaScript学习笔记-构造函数

    什么是构造函数 简单说构造函数是类函数,函数名与类名完全相同,且无返回值.构造函数是类的一个特殊成员函数. JavaScript构造函数 * 在JavaScript的世界里没有类的概念,JavaScr ...

  9. Centos6.5 DNS配置

    服务器端:192.168.186.130 1.安装 # yum -y install bind* 2.主要配置文件 [root@localhost named]# vim /etc/named.con ...

  10. mysql crete view

    CREATE VIEW user_algo_view ASselect `u`.`userId` AS `UserId`,`u`.`userCode` AS `UserCode`,group_conc ...