当前位置:网站首页>leetcode-140:单词拆分 II
leetcode-140:单词拆分 II
2022-07-21 23:16:00 【菊头蝙蝠】
leetcode-140:单词拆分 II
题目
给定一个字符串 s 和一个字符串字典 wordDict ,在字符串 s 中增加空格来构建一个句子,使得句子中所有的单词都在词典中。以任意顺序 返回所有这些可能的句子。
注意:词典中的同一个单词可能在分段中被重复使用多次。
示例 1:
输入:s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
输出:["cats and dog","cat sand dog"]
示例 2:
输入:s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
输出:["pine apple pen apple","pineapple pen apple","pine applepen apple"]
解释: 注意你可以重复使用字典中的单词。
示例 3:
输入:s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
输出:[]
解题
方法一:哈希集合+dfs
class Solution {
public:
vector<string> res;
unordered_set<string> set;
void dfs(string& s,int startIndex,string&& path){
if(startIndex==s.size()){
res.push_back(path);
return;
}else if(startIndex!=0) path+=" ";//头部和尾部不用加空格
for(int i=startIndex;i<s.size();i++){
string tmp=s.substr(startIndex,i-startIndex+1);
if(set.count(tmp)){
dfs(s,i+1,path+tmp);//以形参方式带入,就不需要手动回溯了
}
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
set=unordered_set<string>(wordDict.begin(),wordDict.end());
dfs(s,0,"");
return res;
}
};
方法二:字典树+dfs
class Trie{
public:
bool isEnd=false;
vector<Trie*> next=vector<Trie*>(26,nullptr);
};
class Solution {
public:
Trie* trie=new Trie();
vector<string> res;
vector<string> wordBreak(string s, vector<string>& wordDict) {
for(string& word:wordDict){
insert(word);
}
dfs(s,0,"");
return res;
}
void dfs(string& word,int startIndex,string&& path){
if(startIndex==word.size()){
res.push_back(path);
return;
}else if(startIndex!=0) path+=" ";
Trie* node=trie;
string tmp="";
for(int i=startIndex;i<word.size();i++){
char c=word[i];
tmp+=c;
if(node->next[c-'a']) node=node->next[c-'a'];
else return;
if(node->isEnd){
dfs(word,i+1,path+tmp);
}
}
}
void insert(string& word){
Trie* node=trie;
for(char c:word){
if(node->next[c-'a']==nullptr){
node->next[c-'a']=new Trie();
}
node=node->next[c-'a'];
}
node->isEnd=true;
}
};
边栏推荐
- Clickhouse installation
- One bite of Stream(9)
- It's a holiday. I need to read books carefully
- "Double business success classic" is newly upgraded and launched! Hot summer new products, waiting for a long time to come!
- “新能源+储能“从数字孪生开始,图扑将智慧电力做到极致
- RecordRTC的视频录制,回放,截图,下载
- (iccv-2021) transfeid: Target Recognition Based on transformer
- (Applied intelligence-2022) transgait: gait recognition and ensemble transformer based on multimodality
- Common life cycle of uniapp
- 直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
猜你喜欢
Addition, deletion, query and modification of [mdsql] table (Advanced)
【MySQL必知必会】 存储过程 | 游标
【C】信息管理系统/通讯录通用模板(介绍静态、动态、文件三个版本)
自动化设备制造行业常见管理难题及解决方案
[MySQL must know and know] stored procedure | cursor
如何安装mysql
One bite of Stream(8)
How to use call, bind and apply correctly
WebService interface test
One bite of Stream(9)
随机推荐
如何正确使用call、bind、apply
[PostgreSQL 15] PostgreSQL 15 improves unique and null
Procurement control: shorten the procurement cycle, reduce costs and increase efficiency from the source
小程序项目总结
MYSQL8存储过程生成日历表以及异常处理
深浅拷贝
边框动态效果实现
Deep analysis of JVM object creation and memory allocation of JVM (9)
《性能之巅第2版》阅读笔记(五)--Disks监测
Border dynamic effect implementation
Recordrtc video recording, playback, screenshot, Download
QScriptEngine官方说明文档
QT creator shortcut keys, with shortcut key configuration method
[QNX hypervisor 2.2 user manual]8.7 virtual i/o (virtio)
AMBA 2 AHB、AMBA 3 AHB(AHB_Lite)和AMBA 5 AHB协议比较
Service (LB) and managed pod
uniapp常用的生命周期
[QNX Hypervisor 2.2用户手册]8.7 虚拟I/O(VIRTIO)
Deep and shallow copy
Pyhton crawls the primary and secondary website pages and saves the crawled image information locally