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 ...
随机推荐
- .NET正则基础之——平衡组
1 概述 平衡组是微软在.NET中提出的一个概念,主要是结合几种正则语法规则,提供对配对出现的嵌套结构的匹配..NET是目前对正则支持最完备.功能最强大的语言平台之一,而平衡组正是其强大 ...
- C# 从数据库中删除,插入,修改 索引选中条目
一.删除 1. ) { var currentSelectIndex = usrListView.SelectedIndex; var item = usrView[currentSelectInde ...
- git subtree用法(转)
git subtree用法 一.使用场景 例如,在项目Game中有一个子目录AI.Game和AI分别是一个独立的git项目,可以分开维护.为了避免直接复制粘贴代码,我们希望Game中的AI子目录与AI ...
- lucene索引日期和数字
1.用途. 索引数字的场景主要有两种:一是把它们当作字符串一样处理,比如“要是搁以前,术士能暴击10000多,有木有!”中的"10000",它和其它的词没什么区别,你可以把它仅仅想 ...
- [转]一步一步asp.net_购物车订单与支付宝
本文转自:http://www.cnblogs.com/mysweet/archive/2012/05/19/2508534.html 最近这几天很忙,一边忙着准备一堆课程设计(8门专业课.....伤 ...
- [APAC]导入图片至Word,然后按规则命名(2/2)
#将所有docx文件改成可读 Set-ItemProperty -Path "e:\screenshot\*.docx" -Name IsReadOnly -Value $fals ...
- Visual Studio 常用插件
一.IndentGuide 缩进线插件:每个缩进块首尾添加虚线,使代码看着整洁. 其他插件:继续推荐几款VisualStudio的插件 二.CodeRush code rush 是微软推出的一款VS2 ...
- Linux文件管理命令
cd /home 进入 '/ home' 目录' cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd 进入个人的主目录 cd ~user1 进入个人的主目录 cd - 返回上次所在的目 ...
- Yii源码阅读笔记(十五)
Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...
- PHP基本使用
本篇说的基本使用包括: php常规的语法 date,number,array等对象的处理方式 程序语言的常规的语法都比较好理解,大致相同.假如你是精于其它解释型语言的程序员,迁移到php几乎不费力气. ...