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 ...
随机推荐
- c++ stack 的使用
(1) stack::empty bool empty ( ) const; 判断是否为空. return Value : true if the container size is 0, false ...
- 9.0 alpha 版安装出现 could not execute command lessc 的问题
解决方案: apt-get install node-less
- 记一本关于thinkphp&&MVC的好书
看过好多书,写thinkphp就蜻蜓点水般,而且语言比较书面.看到了李开涌写的php mvc开发实战.他本人是技术方面的专家,写的书结合了对技术的理解.我读起来感觉收获颇多.比如model这块,我一直 ...
- VS2012网布网站与IIS配置
一 VS发布 1首先是 点击自己创建的 web项目 ,右键发布 2 在之后出现的界面中选择配置文件 , 选择或导入发布配置文件,下拉框,选择新建 跳转到下一步 3 在连接界面中 在发布方法中选择文件 ...
- 各分支Linux的镜像下载地址
https://openstack.redhat.com/Image_resources http://fedoraproject.org/en/get-fedora#clouds https ...
- Java集合之Map接口
Map使用键值对来存储数据,将键映射到值对象,一个映射不能包含重复的键,每一个键最多只能映射到一个值.Map接口的具体实现类:HashMap,Hashtable,TreeMap,LinkedHashM ...
- JavaWEB中读取配置信息
第一种方法是使用java.io和java.util包,缺点是路径的概念要清晰, 例子: Properties prop = new Properties(); InputStream in = get ...
- JavaScript系列:函数调用方式
有关JS的问题,持续更新.. 一,函数调用的4种方式 1,函数调用模式 //下面这种模式叫 “函数调用模式”:窗后window来调用 //函数调用四种方式的基础 //这tm不就是作用域this的问题吗 ...
- php根据IP地址跳转对应的城市,淘宝REST api调用地址直接使用
<?php // 定义一个函数getIP() function getIP(){ global $ip; if (getenv("HTTP_CLIENT_IP")) { $i ...
- The P4 Language Specification v1.0.2 Parser
<p4规范>解析器部分详解 p4解析器是根据有限状态机的思想来设计的. 解析器中解析的过程可以被一个解析图(parser graph)所表示,解析图中所表示的某一个状态(或者说,在P4语言 ...