给定一个非空字符串 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. 关于TMDS

    https://en.wikipedia.org/wiki/Transition-minimized_differential_signaling TMDS,Transition Minimized ...

  2. List、Map、Set 三个接口,存取元素时,各有什么特点

    List与Set都是单列元素的集合,它们有一个功共同的父接口Collection. Set里面不允许有重复的元素, 存元素:add方法有一个boolean的返回值,当集合中没有某个元素,此时add方法 ...

  3. 基于vue的环信基本实时通信功能

    本篇文章借鉴了一些资料,然后在这个基础上,我将环信的实现全部都集成在一个组件里面进行实现: https://blog.csdn.net/github_35631540/article/details/ ...

  4. kafka-node+socket.io 测试配置

    1.安装需要插件 npm install express npm install  socket.io npm install  kafka-node 2.kafkatest.js文件 var exp ...

  5. csp-s模拟测试89

    csp-s模拟测试89 $T1$想了一会儿没什么思路,一看$T2$  $1e18$当场自闭打完暴力就弃了,$T3$看完题感觉要求$lca$和$dep$,手玩了一下样例发现$lca$很显然,$dep$貌 ...

  6. Hadoop 平台搭建

    一.在Linux中安装JDK并配置环境变量 输入javac 查看是否已安装java环境如果没有安装 sudo apt-get install openjdk-7-jdk再次检测 javac修改配置参数 ...

  7. dashboard服务

    1.上传镜像,并导入,打标签 2.创建dashboard的deployment和service apiVersion: extensions/v1beta1 kind: Deployment meta ...

  8. 深入理解Android-清晰的理解Service

    1.什么是Service 2.Service的生命周期 3.Service的工作过程 4.Service的start和bind状态有什么区别? 5.同一个Service,先startService,然 ...

  9. threading线程例子 (27-08)

    利用阻塞的时间空闲去执行另一个子线程 import threading from time import ctime, sleep def music(func): for i in range(2) ...

  10. python调用scikit-learn机器学习

    不支持深度学习和强化学习 numpy介绍: np.eye(n)生成一个n维单元数组 数据预处理: iris数据加载 from sklearn import datasetsiris = dataset ...