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: 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. Word Break II 解题报告(Python & C++)

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

  3. [leetcode]Word Break II @ Python

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

  4. [LeetCode] 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: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. [Leetcode] 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: Word Ladder II 解题报告

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

  9. LeetCode: Word Break II [140]

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

随机推荐

  1. iOS异步图片加载优化与常用开源库分析

    网络图片显示大体步骤: 1.下载图片: 2.图片处理(裁剪,边框等): 3.写入磁盘: 4.从磁盘读取数据到内核缓冲区: 5.从内核缓冲区复制到用户空间(内存级别拷贝): 6.解压缩为位图(耗cpu较 ...

  2. Android 中Webview 自适应屏幕

    随笔 - 478  文章 - 3  评论 - 113 Android 中Webview 自适应屏幕   webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomCon ...

  3. datazen Active Directory AD 配置

    今天苦心经营的datazen 链接AD,文档已经无法吐槽了简单的几句话,根本不够用. 先说一下链接AD 的好处吧, 1 首先免去设置密码的麻烦,因为直接用AD账号的密码. 2 更安全,因为客户可不想自 ...

  4. phonegap 2.8.1 toast

    目录结构如下: 以上三个用红色框勾出的地方是需要修改的文件夹. 首先:添加java代码. 在src目录下新建一个包裹:org.apache.cordova 在该包裹下新建类:ToastPlugin.j ...

  5. iOS和hybird移动端性能

         作为一名写了⑦年代码的程序员,目前我最擅长的领域是IOS的客户端开发,在移动领域的开发时间2年. ⑦年前,我刚入行的时候,曾经认为自己将会永远做一个LINUX 服务端C++程序员,于是花了大 ...

  6. Linq 实现普通sql中 where in 的功能

    user.ProjectIds 的值是使用逗号分隔的 例如:1,2,3 projectList = (from a in projectList where (user.ProjectIds.Spli ...

  7. 巧用Session Manager还原Firefox丢失会话

    今天Firefox Crash之后,我的会话全部丢失了.按照以往来说,Firefox在重新启动之后或者Crash之后会有一个会话还原的页面.但今天确实没有.后来我进行Google查阅,试了很多种办法. ...

  8. 数据结构 --- 线性表学习(php模拟)

    线性表:零个或多个数据元素的有限序列(注:以下都是用的整型数据模拟) 一 顺序存储结构(用一段地址连续的存储单元一次存储线性表的数据元素) 1.1 三个属性:存储空间的起始位置:最大存储容量:当前长度 ...

  9. powerpoint取色器有什么用|ppt取色器使用教程

    在使用powerpoint过程中常常发现一些功能我们很少用到,其实是自己不会用的原因,关于powerpoint取色器有什么用呢?接下来我们一起来学一下ppt取色器使用教程. powerpoint取色器 ...

  10. ios学习-delegate、传值、跳转页面

    ios学习-delegate.传值.跳转页面     1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...