原题链接在这里:https://leetcode.com/problems/word-break-ii/

题目:

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
Output:
[
  "cats and dog",
  "cat sand dog"
]

Example 2:

Input:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
Output:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
Explanation: Note that you are allowed to reuse a dictionary word.

Example 3:

Input:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
Output:
[]

题解:

When it needs all the possible results, it comes to dfs.

Could use memo to prune branches. Use memo means divide and conquer, not iterative.

If cache already has key s, then return list value.

Otherwise, get either head or tail of s, check if it is in the wordDict. If yes, put the rest in the dfs and get intermediate result.

Iterate intermediate result, append each candidate and add to res.

Update cache and return res.

Note: When wordDict contains current s, add it to res. But do NOT return. Since it may cut more possibilities.

e.g. "dog" and "dogs" are both in the result. If see "dogs" and return, it cut all the candidates from "dog".

Time Complexity: exponential.

Space: O(n). stack space O(n).

AC  Java:

 class Solution {
Map<String, List<String>> cache = new HashMap<>();
public List<String> wordBreak(String s, List<String> wordDict) {
List<String> res = new ArrayList<>(); if(s == null || s.length() == 0){
return res;
} if(cache.containsKey(s)){
return cache.get(s);
} if(wordDict.contains(s)){
res.add(s);
} for(int i = 1; i<s.length(); i++){
String tail = s.substring(i);
if(wordDict.contains(tail)){
List<String> cans = wordBreak(s.substring(0, i), wordDict);
for(String can : cans){
res.add(can + " " + tail);
}
}
} cache.put(s, res);
return res;
}
}

类似Word Break.

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:Word Break II(DP)

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

  3. [LeetCode] Word Break II 拆分词句之二

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

  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 @ Python

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

  6. [Leetcode] word break ii拆分词语

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

  7. [LeetCode] Word Break II (TLE)

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

  8. LeetCode: Word Break II [140]

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

  9. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

随机推荐

  1. 【转载】Erlang 中 link 和 monitor 的区别

    Link and Monitor differences 原文地址 Introduction link/1 and monitor/2 are 2 different ways of notifyin ...

  2. Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合例子(附完整的请假流程例子,jbpm基础,常见问题解决)

    Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子(附完整的请假流程例子). 1.jbpm4.4 测试环境搭建 2.Jbpm4.4+hibernat ...

  3. CKEDITOR使用与配置

    安装: 下载CKEDITOR的文件,解压后复制到工程的WEBROOT目录下就OK! 引用CKEDITOR的JS文件: 新建JSP页面,添加其JS文件<script type="text ...

  4. SecureCRT的相关问题

    1. 中文显示乱码的解决方法 2. 显示Linux中的颜色信息 3. 解决终端长时间无输入导致SSH连接中断的问题 4. 以公钥方式代替密码方式登录服务器 在SecureCRT中创建Public Ke ...

  5. SQL判断语句用法和多表查询

    1.格式化时间sql语句 本例中本人随便做了两张表,和实际不是很相符,只是想说明sql语句的写法. 例1表格式如下: 需求:查询出本表,但需要使time字段的时间格式为yyyy-MM-dd,比如:20 ...

  6. OpenCV学习笔记——滑动条开关

    由于opencv库中并没有专门为开关而设的函数,可以用滑动条做开关 代码: #include<highgui.h> #include<cv.h> int g_switch_va ...

  7. PHP 查询时区与设置时区

    使用以下语句来查询当前时区设置 date_default_timezone_get() 使用以下语句设置当前时间为北京时间 date_default_timezone_set('PRC');

  8. Linux改变文件或目录的访问权限命令

    使用  ll  或  ls -l 指令时 第一列会显示出目录下文件的权限 例如∶ -rw-r-r- 横线代表空许可.r代表只读,w代表写,x代表可执行.注意这里共有10个位置.第一个字符指定了文件类型 ...

  9. 记录一个bug -- sprintf

    #include<iostream> #include<stdio.h> int main () { char buf[10] = {0}; sprintf(buf," ...

  10. 超级有用的9个PHP代码片段

    在开发网站.app或博客时,代码片段可以真正地为你节省时间.今天,我们就来分享一下我收集的一些超级有用的PHP代码片段.一起来看一看吧! 1.创建数据URI 数据URI在嵌入图像到HTML / CSS ...