[LeetCode] Word Break II 解题思路

时间:2022-09-19 00:15:15

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

这道题是 Word Break的一个拓展

问题:根据给的单词字典,求一个字符串所有可能的有效分割。

有了 Word Break 的经验,这道题做起来也顺畅了许多。

假设将字符串 s 分割为两段,[0,i-1], [i, n-1],如果[0, i-1] 为有效单词,[i, n-1]为有效单词集合,那么 s 就是一个有效字符串。将 [0, i-1] 依次和 [i, n-1] 所有可能分别匹配,则得到 s 以 i 为分割点得全部有效分割。

将 i 从 1 到 n-1 遍历一次,则求得 s 的全部分割的可能。

和 Word Break 一样,需要记录已计算的结果,提高效率。利用 map<int, vector<string>> idx_words[k] 来记录 [k, n-1] 子串的全部有效分割。

    map<int, vector<string>> idx_words;

    vector<string> match(string s, unordered_set<string>& wordDict, int startIdx){
vector<string> res; for (int i = ; i < s.size(); i++) {
string leftS = s.substr(, i); unordered_set<string>::iterator us_iter = wordDict.find(leftS); if (us_iter != wordDict.end()) { int rightRLIdx = i + startIdx; vector<string> rightV;
if (idx_words.find(rightRLIdx) != idx_words.end()){
rightV = idx_words[rightRLIdx];
}else{ string rightS = s.substr(i, s.size() - i); rightV = match(rightS, wordDict, rightRLIdx);
idx_words[rightRLIdx] = rightV;
} for (int ii = ; ii < rightV.size(); ii++) {
string tmpS = leftS + " " + rightV[ii]; res.push_back(tmpS);
}
}
} if (wordDict.find(s) != wordDict.end()) {
res.push_back(s);
} return res;
} vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<string> res; res = match(s, wordDict, ); return res;
}

[LeetCode] Word Break II 解题思路的更多相关文章

  1. LeetCode&colon; Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  2. 【LeetCode】140&period; Word Break II 解题报告(Python & C&plus;&plus;)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  3. &lbrack;leetcode&rsqb;Word Break II &commat; Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  4. &lbrack;LeetCode&rsqb; 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&colon;Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  6. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  7. &lbrack;Leetcode&rsqb; word break ii拆分词语

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. LeetCode&colon; Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  9. LeetCode&colon; Word Break II &lbrack;140&rsqb;

    [题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...

随机推荐

  1. TestLink学习四:TestLink1&period;9&period;13使用说明

    前言 测试管理工具,是指用工具对软件的整个测试输入.执行过程和测试结果进行管理的过程.可以提高回归测试的效率.大幅提升测试时间.测试质量.用例复用.需求覆盖等. TestLink用于进行测试过程中的管 ...

  2. C&num;打开指定路径文件对话框

    private string OpenFileDlog(string DeafultDir) { OpenFileDialog Ofd = new OpenFileDialog(); Ofd.AddE ...

  3. Javascript中的一种深复制实现

    在javascript中,所有的object变量之间的赋值都是传地址的,可能有同学会问哪些是object对象.举例子来说明可能会比较好: typeof(true) //"boolean&qu ...

  4. 浏览器的CSS Hacks

    LZ注:此文原作者是:Paul Irish(Google的前端开发工程师),本文是原文的部分译文. 我不再使用CSS Hacks了,相反的是,我将使用IE的条件判断将类应用到body标签.   但是, ...

  5. Maven之(九)依赖关系

    在maven的管理体系中,各个项目组成了一个复杂的关系网,但是每个项目都是平等的,是个没有贵贱高低,众生平等的世界,全球每个项目从理论上来说都可以相互依赖.就是说,你跟开发spring的大牛们平起平坐 ...

  6. ecshop 分页小记

    ecshop 分页是ajax请求的,必须在主文件里有个 act = query 处理,分页会请求这个act <?php //获取列表 if($_REQUEST['act']=='list'){ ...

  7. UE4实现描边效果

    描边效果属于常见常用的功能,现VR项目中,也需要射线选中一个物体,使物体高亮. 于是在网上找了部分资料,同时也感谢群里的一位大神的提点,总算将描边的功能实现了,这里也写一个简单的示例步骤. 1.我并不 ...

  8. redis对string进行的相关操作

    redis对string类型操作的相关命令以及如何在python使用这些命令 redis对string类型操作的命令: 命令 语法 概述 返回值 Redis SET 命令  set key value ...

  9. cocos2d-x学习记录2——CCAction动作

    CCAction能够使CCNode运动起来,能够呈现出多种多样的动作.这些动作能够改变其运动方向.形状.大小.旋转等. 同时,还可利用CCCallFunc.CCCallFuncN.CCCallFunc ...

  10. C语言 &&num;183&semi; 十进制数转八进制数

    算法训练 十进制数转八进制数   时间限制:1.0s   内存限制:512.0MB      编写函数把一个十进制数输出其对应的八进制数. 样例输入 9274 样例输出 22072   #includ ...