当前位置:网站首页>LeetCode 0139. 单词拆分
LeetCode 0139. 单词拆分
2022-07-26 17:31:00 【Tisfy】
【LetMeFly】139.单词拆分
力扣题目链接:https://leetcode.cn/problems/word-break/
给你一个字符串 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
提示:
1 <= s.length <= 3001 <= wordDict.length <= 10001 <= wordDict[i].length <= 20s和wordDict[i]仅有小写英文字母组成wordDict中的所有字符串 互不相同
方法一:dp
用 d p [ i ] dp[i] dp[i]表示字符串的前 i i i个字母能否由字典中的单词拼接出来。
初始值dp[0] = true
用 n n n代表待拼接字符串的长度
第一维循环 i i i从 1 1 1到 n n n,依次判断待拼接字符串的前 i i i个字母能否被拼接。
第二维循环 j j j从 0 0 0到 i − 1 i - 1 i−1,依次判断前i个字母能否由已验证的能被拼接出来的前j个字母和存在于字典中的 由 第j个到第i个字母 组成的单词拼接而成。
如果dp[j]==true并且原字符串的子串[j, i]存在于字典中,就把dp[i]标记为true
- 时间复杂度 O ( n 2 ) O(n^2) O(n2)
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> se;
for (string& s : wordDict) {
se.insert(s);
}
vector<bool> dp(s.size() + 1, false);
dp[0] = true;
for (int i = 1; i <= s.size(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && se.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125991942
边栏推荐
- Spark统一内存划分
- PMP Exam details, what changes have been made to the new exam outline?
- COSCon'22城市/学校/机构出品人征集令
- 【集训Day3】delete
- [training Day1] spy dispatch
- Cross Site Request Forgery (CSRF)
- [static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
- Click hijacking attack
- AI zhetianchuan ml integrated learning
- [Day2] cinema ticket
猜你喜欢

How to set IP for layer 2 management switches
![[Oumi reading club] talk about the creator economy in the meta universe: infinite dimension](/img/60/17cb0295f81dc580cc3ff8543ec253.png)
[Oumi reading club] talk about the creator economy in the meta universe: infinite dimension

Redisdesktopmanager removes the upgrade prompt

解决哈希冲突的几种方式

线性回归——以一道等差数列的题为例

Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming!

Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge

2、 Topic communication principle, code implementation

面试OPPO,16道题甩过来,我人傻了
![[training Day2] sculpture](/img/d9/2e2ee8b4d995a29204afba889da635.png)
[training Day2] sculpture
随机推荐
2022河南萌新联赛第(三)场:河南大学
8.2 一些代数知识(群、循环群和子群)
Hosts this file has been set to read-only solution
第17周自由入侵 指针练习--输出最大值
JS closure simulates private variable interview questions and immediately executes function Iife
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
Linux Installation mysql8.0.29 detailed tutorial
【集训Day3】Reconstruction of roads
1、 C language program structure, compilation and operation, data type related
RedisDesktopManager去除升级提示
Vector CANoe Menu Plugin拓展入门
SQL中去去重的三种方式
【Unity3D】摇杆
线性回归——以一道等差数列的题为例
[day3] reconstruction of roads
Diagram of seven connection modes of MySQL
Relative path and absolute path
hosts该文件已设置为只读的解决方法
2022 Henan Mengxin League game (3): Henan University
LeetCode50天刷题计划(Day 4—— 最长回文子串 14.00-16:20)