给定一个非空字符串 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. 异或前缀和——cf1186C

    很思维的题,b的1个数和c的1个数相差为偶数时,必定有偶数个不同 反之必定有奇数个不同 #include <iostream> #include <cstdlib> #incl ...

  2. 线性dp,后缀处理——cf1016C好题

    绝对是好题 #include<bits/stdc++.h> using namespace std; #define maxn 300005 #define ll long long ll ...

  3. CF 1063B Labyrinth

    传送门 解题思路 看上去很简单,\(bfs\)写了一发被\(fst\)...后来才知道好像一群人都被\(fst\)了,这道题好像那些每个点只经过一次的传统\(bfs\)都能被叉,只需要构造出一个一块一 ...

  4. svg实现渐变进度圆环

    效果图 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="ut ...

  5. thinkphp一键清除缓存的方法

    后台控制器: <?php namespace Home\Controller; use Think\Controller; class HuancuController extends Cont ...

  6. MongoDB错误记录

    文章目录 mongoDB启动报错 mongoDB启动报错 在bin目录下执行 ./mongod 报错如下 F CONTROL [main] Failed global initialization: ...

  7. PyCharm中批量查找及替换

    选中需要操作的字符 Ctrl + R 替换 Ctrl + Shift + F 全局查找 Ctrl + Shift + R 全局替换 源自: PyCharm中批量查找及替换 - Ella_Wu - 博客 ...

  8. HTTP协议响应篇

    http响应的基本介绍 一个HTTP响应代表服务器向客户端回送的数据, 由三个部分构成 状态行[200 , 302 304, 403, 404, 500] 响应消息头 返回的实体内容 http响应状态 ...

  9. IDA 远程调试设置

    第一步,先去 IDA   dbgsrv  这个目录下,找到要调试的那个远程计算机对应的可用客户端, 比如,android_server, 把它拷贝到目标计算机中, 比如 adb push .... 然 ...

  10. 论文阅读笔记---HetConv

    1 写在前边的话 HetConv性能:当使用HetConv取代标准卷积之后,FLOPs大概是之前的1/8到1/3,更重要的是精度几乎不变!!! 论文地址:https://arxiv.org/abs/1 ...