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. PROTEUS串口仿真遇到的BUG(转载)

    转载自:http://blog.csdn.net/viperchaos/article/details/6246419 串口通信设置的顺序非常重要,最好严格按照步骤来,在开启数据接受的使能之前最好一定 ...

  2. git推送代码问题之:ERROR: [abcdefg] missing Change-Id in commit message footer

    一.问题: 在日常的工作中,使用git推送代码时会出现以下报错,“missing Change-Id in commit message” : qinjiaxi:$ git push origin H ...

  3. C# 微信公众平台开发(5)--添加图文素材

      微信公众平台开发 --添加素材 关于微信公众号素材管理,我们可以通过接口文档,了解基本详情:http://mp.weixin.qq.com/wiki/10/10ea5a44870f53d79449 ...

  4. 敏捷为什么会失败之「PA-SA-WAKA-DA」理论

    在日常生活中,有种有趣的现象:我们更津津乐道于美好的故事,比如提到好莱坞,我们关注的只是大牌明星,却忽略了他们成名其背后的艰辛.对于那些成功的敏捷项目,也是如此.在我们见证成功的同时,却忘记了项目团队 ...

  5. hdu2138 How many prime numbers 米勒测试

    hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...

  6. Kali Linux 2020.1安装以及安装后要做的事

    Kali Linux是基于Debian的Linux发行版,预装了许多渗透测试软件,让大家从各种繁琐的软件安装中解脱出来,专注于测试本身. 本文章介绍了如何安装目前最新的2020.1版本,以及安装好后补 ...

  7. 走迷宫(二):在XX限制条件下,是否走得出

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 解题方法:BFS+访问数组vis[][]; 给你起点位置和终点位置,让你判断能不能到达,并且拐弯 ...

  8. Poj2965 冰箱的开关

    #include<iostream> using namespace std; int flag; int step; ][]; ] = { }; ] = { }; void turn(i ...

  9. C++98/11/17表达式类别

    目标 以下代码能否编译通过,能否按照期望运行?(点击展开) #include <utility> #include <type_traits> namespace cpp98 ...

  10. java——定时任务

    java定时任务直接看代码 public void timeTask(){ Timer timer = new Timer(); timer.schedule(new TimerTask() { pu ...