给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

说明:

  • 拆分时可以重复使用字典中的单词。
  • 你可以假设字典中没有重复的单词。

示例 1:

输入: s = "leetcode", wordDict = ["leet", "code"] 输出: true 解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。

示例 2:

输入: s = "applepenapple", wordDict = ["apple", "pen"] 输出: true 解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。   注意你可以重复使用字典中的单词。

示例 3:

输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] 输出: false

dp[i]表示长为i的从s的第一个字符开始的字串是否能匹配。

可以想为长度为i是否匹配需要看长度i - j是否匹配和是否有长度为j且合适正确的单词。

这样就形成了动态规划的公式。

class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict)
{
int len = s.size();
vector<bool> dp(len + 1, false);
map<string, bool> check;
//i表示结束的位置,相当于end()方法;
for(int i = 0; i < wordDict.size(); i++)
{
check[wordDict[i]] = true;
}
dp[0] = true;
for(int i = 1; i <= len; i++)
{
for(int j = 0; j < i; j++)
{
if(dp[j] && check[string(s.begin() + j, s.begin() + i)])
{
dp[i] = true;
break;
}
}
}
return dp[len];
}
};

Leetcode139. Word Break单词拆分的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  3. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

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

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

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

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

  6. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  7. LeetCode139:Word Break

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

  8. Java Word Break(单词拆解)

    给定一个字符串 String s = "leetcode" dict = ["leet", "code"]. 查看一下是够是字典中的词语组成 ...

  9. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

随机推荐

  1. 关于 wpf 的ICommand 的 CanExecute CanExecuteChanged func action的认识

    关于 wpf 的ICommand 的 CanExecute CanExecuteChanged  func  action的认识

  2. 【JZOJ6346】ZYB和售货机

    description analysis 其实这个连出来的东西叫基环内向树 先考虑很多森林的情况,也就是树根连回自己 明显树根物品是可以被取完的,那么买树根的价钱要是儿子中价钱最小的那个 或者把那个叫 ...

  3. php+jquery 上拉加载

    <script type="text/javascript"> var resflow = true,pages =2; var ps=$("#ids&quo ...

  4. 浅析ES的_source、_all、store、index

    Elasticsearch中有大量关键概念容易混淆,对于初学者来说是噩梦: _source字段里存储了什么? index属性的作用是什么? 何时应该开启_all字段? store属性和_source字 ...

  5. day 64 Django基础十之Form和ModelForm组件

    Django基础十之Form和ModelForm组件   本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Mod ...

  6. 17.splash_case02

    # 抓取<我不是药神>的豆瓣评论 import csv import time import requests from lxml import etree fw = open('doub ...

  7. [PKUSC2018]神仙的游戏

    题目 画一画就会发现一些奇诡的性质 首先如果\(len\)为一个\(\operatorname{border}\),那么必然对于\(\forall i\in [1,len]\),都会有\(s_i=s_ ...

  8. 16-1-es5

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. js 实现加载百分比效果

    效果: html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  10. 2019-2-17-如何在-Windows-10-中移除-Internet-Explorer-浏览器

    title author date CreateTime categories 如何在 Windows 10 中移除 Internet Explorer 浏览器 lindexi 2019-02-17 ...