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. Java中Error和Exception的异同以及运行时异常(Runtime exception)与检查型异常(checked exception)的区别

    一:Error和Exception的基本概念: 首先Exception和Error都是继承于Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕 ...

  2. JDBC12 ORM01 Object[]存放一条记录

    ORM(Object Relationship Mapping)的基本思想 -表结构跟类对应:表中的字段和类的属性对应:表中记录和对象对应 让JavaBean的属性名和类型尽量和数据库保持一致 一条记 ...

  3. 【Docker】在本地打包maven程序为docker镜像报错: Connect to localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1]

    错误信息: [ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (default-cli) on pr ...

  4. LiteAI四大技术"杀手锏",解锁物联网智能设备AI开发难关

    你知道我们生活中常见的物联网智能设备融合AI技术后,会给我们带来什么样的智能交互体验?在我们指尖触碰的那一刹那背后隐藏的代码世界又是怎么样的呢? 今天就来和大家说说IoT智能设备轻松实现AI的奥秘! ...

  5. vue element-ui el-form-item 循环渲染,验证表单内容

    data里面如下图:

  6. nginx default server

    配合server_name _ 可以匹配所有的域名,在设置default server 可以轻松屏蔽一些非域名访问的请求. 配置如下 server { listen 80 default_server ...

  7. 字符串 kmp算法 codeforce 625B 题解(模板)

    题解:kmp算法 代码: #include <iostream>#include <algorithm>#include <cstring>#include < ...

  8. intellij tomcat

    VMOption -server -XX:PermSize=128M -XX:MaxPermSize=256m

  9. redis学习——day01_redis简介与安装

    一.Redis 简介 1.1 Redis是什么 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redi ...

  10. Windows Terminal安装并美化

    介绍 Windows Teminal是一款新式.快速.高效.强大的终端应用程序,适用于命令行工具.命令提示符.PowerShell.WSL(Linux子系统)等等的Shell用户,主要功能包括多选项卡 ...