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. JDBC11 封装+资源配置文件管理

    封装为Utils类+程序资源文件去配置 public class Utils { static Properties p=null; static { p=new Properties(); try ...

  2. go多种uuid生成方式

    package main import ( "fmt" "github.com/chilts/sid" "github.com/kjk/betterg ...

  3. Java 在Excel中创建透视表

    本文内容介绍通过Java程序在Excel表格中根据数据来创建透视表. 环境准备 需要使用Excel类库工具—Free Spire.XLS for Java,这里使用的是免费版,可通过官网下载Jar包并 ...

  4. html中require.config 缓存问题

    在html中,require的官方基本用法如下: <!DOCTYPE html> <html> <head> <title>My Sample Proj ...

  5. python --RecursionError: maximum recursion depth exceeded in comparison

    在学习汉娜塔的时候,遇到一个error RecursionError: maximum recursion depth exceeded in comparison 经过百度,百度的方法: 加上: i ...

  6. Lr运行错误Error: Socket descriptor not found. Hint: the problem might be

    在controller中,运行时,报如下错误“Error: Socket descriptor not found.  Hint: the problem might be solved applyi ...

  7. C# 操作Orcle数据库

    1.首先添加NuGet:Oracle.ManagedDataAccess 2.配置连接数据库字符串:Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(H ...

  8. 阿里云服务器 ECS Ubuntu系统下PHP,MYSQL,APACHE2的安装配置

    1.系统更新,必须更新,否则有些软件会找不到. apt-get update apt-get upgrade 2.安装mysql sudo apt-get install mysql-server 3 ...

  9. 干货!JNPF快速开发平台功能一览

      JNPF,采用主流的两大技术Java/.Net开发,是一套低代码开发平台,可视化开发环境,有拖拽式的代码生成器,灵活的权限配置.SaaS服务,强大的接口对接,随心可变的工作流引擎,一站式开发多端使 ...

  10. centOS 6.8下使用Gparted进行分区扩容

    centOS 6.8下使用Gparted进行分区扩容 ​ 机器环境:windows上运行的VMware虚拟机,系统为centOS 6.8. ​ 由于前期分区分配空间过小,无法满足后续的数据存储预期,所 ...