题目链接 : 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. Kotlin使用率达35%,Java要退位了?

    在今年的Google I/O大会上,关于Kotlin,Google只说了只言片语: 在过去一年里,有35%的专业Android开发者在使用Kotlin,其中95%的开发者都对Kotlin非常满意. 之 ...

  2. sh_06_函数的返回值

    sh_06_函数的返回值 def sum_2_num(num1, num2): """对两个数字的求和""" result = num1 + ...

  3. [CSP-S模拟测试]:电压机制(图论+树上差分)

    题目描述 科学家在“无限神机”($Infinity\ Machine$)找到一个奇怪的机制,这个机制有$N$个元件,有$M$条电线连接这些元件,所有元件都是连通的.两个元件之间可能有多条电线连接.科学 ...

  4. HBase调优案例(二)——高并发下bulkload出现超时

    原因分析: 导入数据——>HBase,在客户端会发生非常多的rpc请求到regionServer,从而加大regionServer上的压力,如果regionServer比较忙碌(handle被占 ...

  5. hibernate UML图

  6. 如果将get请求转换成post请求

    td><a href="emp/${emp.id}">Edit</a></td> <form action="" ...

  7. Lombok 注解简介

    Lombok @AllArgsConstructor /** * 生成一个包含所有属性的构造函数 */ @Target(ElementType.TYPE) @Retention(RetentionPo ...

  8. hdu6599 I Love Palindrome String

    由样例可知,题目中求的回文串数量,其实是本质不同的回文串数量,这个可以直接用回文树来做. 考虑前半段是回文串这个限制,这个东西回文树不好做,可以再套一个马拉车,然后记录一下插入到回文树的节点中最后一个 ...

  9. 基于注解的springmvc开发

    原理简析 1. 背景知识:org.springframework.web.ServletContainerInitializer接口 在基于注解的servlet开发中,ServletContainer ...

  10. 有关MSSQL2000在Win7上的安装

    https://baijiahao.baidu.com/s?id=1593533837896849226&wfr=spider&for=pc 怎么在win7下安装sql server2 ...