LeetCode Word Break II
原题链接在这里: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的更多相关文章
- 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 ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [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 ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- 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 ...
随机推荐
- filter:alpha(opacity=100,style=1)
filter:alpha(opacity=100,style=1) 1.opacity属性:设置透明度,取值0至100之间的任意数值,100表示完全不透明: 2.style属性:设置渐变风格: 0表示 ...
- 运行时(iOS)
运行时(iOS) 一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得 ...
- [FollowUp] Combinations 组合项
这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...
- CSS3 Media Queries在iPhone4和iPad上的运用
CSS3 Media Queries的介绍在W3CPlus上的介绍已有好几篇文章了,但自己碰到的问题与解决的文章还是相对的较少.前几天在<修复iPhone上submit按钮bug>上介绍了 ...
- post可以直接把get请求代入到目标url中
Feigong --非攻 非攻 取自<秦时明月>--非攻,针对不同情况自由变化的武器 Feigong,针对各种情况自由变化的mysql注入脚本 Feigong,In view of the ...
- NBOJv2 Problem 1009 蛤玮的魔法(二分)
Problem 1009: 蛤玮的魔法 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %ll ...
- [kuangbin带你飞]专题八 生成树 - 次小生成树部分
百度了好多自学到了次小生成树 理解后其实也很简单 求最小生成树的办法目前遇到了两种 1 prim 记录下两点之间连线中的最长段 F[i][k] 之后枚举两点 若两点之间存在没有在最小生成树中的边 那么 ...
- 优雅的函数式编程--Clojure概述
欢迎转载,转载请注明出处,徽沪一郎. 楔子 由于阅读storm源码的原因,头一次接触到Clojure.没有花特别的时间来研究clojure语法,只是在一些特殊的用法时,才查了一下clojure官网的文 ...
- Open vSwitch使用案例扩展实验
参考:Open vSwitch使用案例扩展实验 实验目的: 通过python脚本调用OpenvSwitch命令: 学习Mininet基于python脚本创建拓扑的实现: 进一步深度使用"ov ...
- PHP 设计模式 笔记与总结(10)数据对象映射模式 2
[例2]数据对象映射模式结合[工厂模式]和[注册模式]的使用. 入口文件 index.php: <?php define('BASEDIR',__DIR__); //定义根目录常量 includ ...