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 ... 
随机推荐
- NSJSONSerialization介绍
			ios5中apple增加了解析JSON的api——NSJSONSerialization.网上已经有人做过测试,NSJSONSerialization在效率上完胜SBJSON.TouchJSON. ... 
- hdu Train Problem I
			这道题是道简单的栈模拟题,只要按照真实情况用栈进行模拟即可: #include<stdio.h> #include<string.h> #include<stack> ... 
- Why Consumer Hardware Start-ups Fail
			今年看到一篇文章还是很受启发. If you have the guts to start selling what you believe in, customers who share your ... 
- 【iHMI43 4.3寸液晶模块】demo例程(版本1.02)发布
			============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ... 
- JavaWEB中读取配置信息
			第一种方法是使用java.io和java.util包,缺点是路径的概念要清晰, 例子: Properties prop = new Properties(); InputStream in = get ... 
- 三层交换单臂路由vlan间通信综合实验之降龙要点--Lee
			单臂路由三层交换机提供vlan间的通信之菜鸟之降龙详解要点: 图示 PC:左到右依次设置IP172.16.10.1, 20.1, 30.1, 40,1 ,50,1 /24 网关10.2 ... 
- ajax中文传送到模板显示为null
			问题: 名称空间声明语句必须是第一个语句中的脚本: Fatal error: Namespace declaration statement has to be the very first stat ... 
- Visual Studio快捷键不能使用解决办法
			环境: Visual Studio 2010,windows 7 使用Visual Studio查找变量或方法时常用到[定位到]功能 但该功能的快捷键却不能使用,解决办法如下所示: 1.工具--> ... 
- 微信公众账号开发教程(三) 实例入门:机器人(附源码) ——转自http://www.cnblogs.com/yank/p/3409308.html
			一.功能介绍 通过微信公众平台实现在线客服机器人功能.主要的功能包括:简单对话.查询天气等服务. 这里只是提供比较简单的功能,重在通过此实例来说明公众平台的具体研发过程.只是一个简单DEMO,如果需要 ... 
- Grid组件 列头居中
			在grdMain组件中的 class中设置“ x-grid-title-center ” 
