140. 单词拆分 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”]

输出:

[]

class Solution {
private Map<String, List<String>> cache = new HashMap<>(); public List<String> wordBreak(String s, List<String> wordDict) {
return dfs(s, wordDict,0);
} private List<String> dfs(String s, List<String> wordDict, int offset){ if (offset == s.length()){
List<String> res = new ArrayList<>();
res.add("");
return res;
} if (cache.containsKey(s.substring(offset))){
return cache.get(s.substring(offset));
} List<String> res = new ArrayList<>();
for (String word : wordDict){
if (word.equals(s.substring(offset, Math.min(s.length(),offset + word.length())))){
List<String> next = dfs(s, wordDict, offset + word.length());
for (String str: next){
res.add((word + " "+ str).trim());
}
}
} cache.put(s.substring(offset),res);
return res;
}
}

Java实现 LeetCode 140 单词拆分 II(二)的更多相关文章

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

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

  2. [LeetCode] 140. 单词拆分 II

    题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...

  3. Java实现 LeetCode 212 单词搜索 II(二)

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

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

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

  5. Java实现 LeetCode 139 单词拆分

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

  6. Java for LeetCode 140 Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  7. 140. 单词拆分 II

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

  8. Java实现 LeetCode 212 单词搜索 II

    public class Find2 { public int[] dx={1,-1,0,0}; public int[] dy={0,0,1,-1}; class Trie{ Trie[] trie ...

  9. Java实现LeetCode 139 单词拆分

    public boolean wordBreak(String s, List<String> wordDict) { if(s.length() == 0){ return false; ...

随机推荐

  1. 【hdu1007】最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 分治法的经典应用,复杂度可以证明为nlognlogn #include <iostream> ...

  2. [hdu1532]最大流

    裸最大流,求最大流一般步骤如下: (1)所有正向边权初始化为容量,反向边权初始化为0 (2)找增广路 (3)找到则进入(4),否则得到最大流并退出 (4) 增广路上所有边减去最小边权,相应的方向边加上 ...

  3. python--遇到SyntaxError: Non-UTF-8 code starting with '\xb8' in file

    在运行python中因为添加了中文注释,遇到SyntaxError: Non-UTF-8 code starting with '\xb8' in file 经过百度,说是Python的默认编码格式是 ...

  4. jquery-----ajax的几种方法

    $.get(url,function(data,status){ if(status == 'success'){ layer.msg(data,{shift:1,time:2000},functio ...

  5. Dubbo对Spring Cloud说:来老弟,我要拥抱你

    项目地址 https://github.com/yinjihuan/kitty-cloud 前言 Kitty Cloud 开源后有以为朋友在 GitHub 上给我提了一个 issues,问为什么项目中 ...

  6. c#word文档输出

    在工作中有时需要把内容用word文档展示出来 在写代码前要引用word的dll Microsoft.Office.Interop.Word“ sing System; using System.Col ...

  7. SpringBoot整合Redis实现简单的set、get

    一.导入pom.xml文件相关的依赖并配置 <dependency> <groupId>org.springframework.boot</groupId> < ...

  8. 在 n 道题目中挑选一些使得所有人对题目的掌握情况不超过一半。

    Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...

  9. 纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例

    查看完整文章点击原文链接:纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例 你是否还在用postman\jmeter做接口自动化吗?用python的开源框架[unit ...

  10. jdk8 Collections#sort究竟做了什么

    前言 Collections#sort 追踪代码进去看,会调用到Arrays.sort,看到这里时,你肯定会想,这不是很简单,Arrays.sort在元素较少时使用插入排序,较多时使用快速排序,再多时 ...