leetcode-140:单词拆分 II

简介: 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;
    }
};
相关文章
|
6月前
|
机器学习/深度学习 算法 Go
【LeetCode 热题100】139:单词拆分(动态规划全解析+细节陷阱)(Go语言版)
本题是 LeetCode 热题 139:单词拆分(Word Break),需判断字符串 `s` 是否能由字典 `wordDict` 中的单词拼接而成。通过动态规划(DP)或记忆化搜索解决。DP 中定义布尔数组 `dp[i]` 表示前 `i` 个字符是否可拆分,状态转移方程为:若存在 `j` 使 `dp[j]=true` 且 `s[j:i]` 在字典中,则 `dp[i]=true`。初始条件 `dp[0]=true`。代码实现中用哈希集合优化查找效率。记忆化搜索则从起始位置递归尝试所有切割点。两种方法各有利弊,DP 更适合面试场景。思考扩展包括输出所有拆分方式及使用 Trie 优化大字典查找。
189 6
|
存储 自然语言处理 算法
☆打卡算法☆LeetCode 140. 单词拆分 II 算法解析
☆打卡算法☆LeetCode 140. 单词拆分 II 算法解析
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 139. 单词拆分 算法解析
☆打卡算法☆LeetCode 139. 单词拆分 算法解析
|
算法
代码随想录算法训练营第四十六天 | LeetCode 139. 单词拆分、多重背包、背包总结
代码随想录算法训练营第四十六天 | LeetCode 139. 单词拆分、多重背包、背包总结
151 1
|
算法 测试技术
代码随想录 Day39 动态规划 LeetCode T139 单词拆分 动规总结篇1
代码随想录 Day39 动态规划 LeetCode T139 单词拆分 动规总结篇1
133 0
【LeetCode 热题 HOT 100】139. 单词拆分【中等】
【LeetCode 热题 HOT 100】139. 单词拆分【中等】
|
Python
[Leetcode][Python]Word Break/Word Break II/单词拆分/单词拆分 II
Word Break 题目大意 给定一个目标字符串和一组字符串,判断目标字符串能否拆分成数个字符串,这些字符串都在给定的那组字符串中。 解题思路 动态规划
226 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
211 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
155 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
328 2