题目链接 : https://leetcode-cn.com/problems/word-break-ii/

题目描述:

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

  • 分隔时可以重复使用字典中的单词。
  • 你可以假设字典中没有重复的单词。

示例:

示例 1:

输入:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
[
"cats and dog",
"cat sand dog"
]

示例 2:

输入:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
输出:
[
"pine apple pen apple",
"pineapple pen apple",
"pine applepen apple"
]
解释: 注意你可以重复使用字典中的单词。

示例 3:

输入:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
输出:
[]

思路:

动态规划:

自顶向下:

参照139. 单词拆分 | 题解链接

相关题型:139. 单词拆分

代码:

思路一:

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
import functools
if not wordDict:return []
wordDict = set(wordDict)
max_len = max(map(len, wordDict))
@functools.lru_cache(None)
def helper(s):
res = []
if not s:
res.append("")
return res
for i in range(len(s)):
if i < max_len and s[:i+1] in wordDict:
for t in helper(s[i+1:]):
if not t:
res.append(s[:i+1])
else:
res.append(s[:i+1] + " " + t)
return res
return helper(s)

java

class Solution {
public List<String> wordBreak(String s, List<String> wordDict) {
int max_len = 0;
for (String word : wordDict) max_len = Math.max(max_len, word.length());
return helper(s, max_len, wordDict, new HashMap<String, LinkedList<String>>());
} private List<String> helper(String s, int max_len, List<String> wordDict, HashMap<String, LinkedList<String>> cache) {
if (cache.containsKey(s)) return cache.get(s);
LinkedList<String> res = new LinkedList<>();
if (s.length() == 0) {
res.add("");
return res;
}
for (int i = 0; i < s.length(); i++) {
if (i < max_len && wordDict.contains(s.substring(0, i + 1))) {
for (String tmp : helper(s.substring(i + 1), max_len, wordDict, cache))
res.add(s.substring(0, i + 1) + (tmp.isEmpty() ? "" : " ") + tmp);
}
}
cache.put(s, res);
return res;
}
}

[LeetCode] 140. 单词拆分 II的更多相关文章

  1. Java实现 LeetCode 140 单词拆分 II(二)

    140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...

  2. Java实现 LeetCode 140 单词拆分II

    class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...

  3. leetcode 140 单词拆分2 word break II

    单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...

  4. 140. 单词拆分 II

    Q: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分隔时可以重复使用字典 ...

  5. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  6. Java实现 LeetCode 139 单词拆分

    139. 单词拆分 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可 ...

  7. [leetcode] 212. 单词搜索 II(Java)

    212. 单词搜索 II 这leetcode的评判机绝对有问题!!同样的代码提交,有时却超时!害得我至少浪费两个小时来寻找更优的答案= =,其实第一次写完的代码就可以过了,靠!!!第207位做出来的 ...

  8. Leetcode 212.单词搜索II

    单词搜索II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻&q ...

  9. Leetcode 139.单词拆分

    单词拆分 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典 ...

随机推荐

  1. Vue的编译过程

      碰到是否有template选项时,会询问是否要对template进行编译: 在template编译(渲染成UI)有一个过程.模板通过编译生成AST,再由AST生成Vue的渲染函数,渲染函数结合数据 ...

  2. create-react-app+react-app-rewired引入antd实践

    注:模块化按此方发npm install antd --save npm install babel-plugin-import --save-dev npm install react-app-re ...

  3. Luogu P2678 跳石头

    题目链接:Click here Solution: 最小值最大,显然二分,二分出mid后贪心去除石头,判断m次内是否可行即可 Code: #include<bits/stdc++.h> # ...

  4. JS常用正则表达式验证

    一.电话+手机 重点是正则表达式: var myreg=/^[1][3,4,5,7,8][0-9]{9}$/; 表达式的意思是: 1--以1为开头: 2--第二位可为3,4,5,7,8,中的任意一位: ...

  5. 命令行创建 vue 项目(仅用于 Vue 2.x 版本)

    1 .安装 Node.js 和 npm ( 验证安装成功输入下图 1 命令行可得 2:输入命令行 3 可得 4 即安装成功) 2.安装全局 webpack (安装依照下图输入命令行 1 耐心等待至到出 ...

  6. 【转】jqprint打印时自定义页眉页脚

    需求:自定义页眉,实现打印时分页时每页页眉都显示相同的信息 打印所用插件jqprint 解决方法: <div class="divHeader"> <span s ...

  7. Nginx的正则表达式

    Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Ра ...

  8. 【Python】学习笔记十二:模块

    模块(module) 在Python中,一个.py文件就是一个模块.通过模块,你可以调用其它文件中的程序 引入模块 先写一个first.py文件,内容如下: def letter(): print(' ...

  9. mysql数据库表反向生成modes类

    一,如果你是windows,打开cmd,切换到desktop目录下 二,需要连接你的数据库,并且执行命令:sqlacodegen  --outfile models.py mysql+pymysql: ...

  10. What is 'typeof define === 'function' && define['amd']' used for?

    What is 'typeof define === 'function' && define['amd']' used for? This code checks for the p ...