当前位置:网站首页>Leetcode 0139. word splitting
Leetcode 0139. word splitting
2022-07-26 18:14:00 【Tisfy】
【LetMeFly】139. Word splitting
Force button topic link :https://leetcode.cn/problems/word-break/
Give you a string s And a list of strings wordDict As a dictionary . Please judge whether you can use the words in the dictionary to splice s .
Be careful : It is not required to use all the words in the dictionary , And the words in the dictionary can be reused .
Example 1:
Input : s = "leetcode", wordDict = ["leet", "code"] Output : true explain : return true because "leetcode" Can be "leet" and "code" Stitching into .
Example 2:
Input : s = "applepenapple", wordDict = ["apple", "pen"] Output : true explain : return true because"applepenapple"Can be"apple" "pen" "apple" Stitching into. Be careful , You can reuse the words in the dictionary .
Example 3:
Input : s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output : false
Tips :
1 <= s.length <= 3001 <= wordDict.length <= 10001 <= wordDict[i].length <= 20sandwordDict[i]Only lowercase letterswordDictAll the strings in Different from each other
Method 1 :dp
use d p [ i ] dp[i] dp[i] Represents the beginning of a string i i i Can the letters be spliced from the words in the dictionary .
Initial value dp[0] = true
use n n n Represents the length of the string to be spliced
First dimensional cycle i i i from 1 1 1 To n n n, Judge the front of the string to be spliced in turn i i i Can letters be spliced .
2D cycle j j j from 0 0 0 To i − 1 i - 1 i−1, Judge in turn front i Letters Whether it can be determined by Verified front that can be spliced j Letters and Exist in the dictionary from The first j One to the first i Letters The words that make up Splicing into .
If dp[j]==true also Substring of the original string [j, i] In the dictionary , Just put dp[i] Marked as true
- Time complexity O ( n 2 ) O(n^2) O(n2)
- Spatial complexity O ( n ) O(n) O(n)
AC Code
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()];
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125991942
边栏推荐
猜你喜欢

PS_1_认识主界面_新建文档(分辨率)_打开保存(序列动画)

The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry

SSH based online mall

PS_2_图层

成为测试/开发程序员,小张:现实就来了个下马威......

Hosts this file has been set to read-only solution

数据仓库:详解维度建模之事实表

Relative path and absolute path

Heavy! The 2022 China open source development blue book was officially released

【集训Day2】cinema ticket
随机推荐
Machine learning by Li Hongyi 2. Regression
7、 Common commands of ROS (II): rosservice, rossrv, rosparam
菜鸟 CPaaS 平台微服务治理实践
Interview with celebrities | open source is a double-edged sword for security -- Wei Jianfan, author of the Chinese translation of cathedral and market
Linux Installation mysql8.0.29 detailed tutorial
跟我学 UML 系统建模
【英雄哥七月集训】第 25天: 树状数组
2022 Henan Mengxin League game (3): Henan University
Relative path and absolute path
How to assemble a registry?
The chess robot broke the finger of a 7-year-old boy because "the chess player violated safety rules"?
[training day3] delete
SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
PMP考生必读,7月30日考试防疫要求都在这里
AI遮天传 DL-回归与分类
[training Day2] sculpture
Zhaoqi science and technology innovation overseas high-level talent introduction platform, entrepreneurship event Roadshow
中国聚异丁烯市场研究与投资价值报告(2022版)
兆骑科创海外高层次人才引进平台,创业赛事活动路演
openssl