import java.util.*;

/**
* Source : https://oj.leetcode.com/problems/word-break-ii/
*
* Given a string s and a dictionary of words dict, add spaces in s to construct a sentence
* where each word is a valid dictionary word.
*
* Return all such possible sentences.
*
* For example, given
* s = "catsanddog",
* dict = ["cat", "cats", "and", "sand", "dog"].
*
* A solution is ["cats and dog", "cat sand dog"].
*/
public class WordBreak2 {
/**
* 找到所有可能的分割方法,使得分割后的单词在dic中
*
* 使用DFS
*
* @param str
* @param dic
* @return
*/
public String[] wordBreak (String str, Set<String> dic) {
List<String> result = new ArrayList<String>();
List<String> solution = new ArrayList<String>();
recursion(str, dic, solution, result);
return result.toArray(new String[solution.size()]);
} private void recursion (String str, Set<String> dic, List<String> solution, List<String> result) {
if (str.length() == 0 && solution.size() > 0) {
StringBuffer stringBuffer = new StringBuffer();
for (String word : solution) {
stringBuffer.append(word);
stringBuffer.append(" ");
}
result.add(stringBuffer.substring(0, stringBuffer.length()-1));
}
for (int i = 0; i < str.length(); i++) {
if (dic.contains(str.substring(0, i+1))) {
solution.add(str.substring(0, i+1));
recursion(str.substring(i+1, str.length()), dic, solution, result);
solution.remove(solution.size()-1);
}
}
} public static void main(String[] args) {
WordBreak2 wordBreak2 = new WordBreak2();
String[] dicStr = new String[]{"cat", "cats", "and", "sand", "dog"};
System.out.println(Arrays.toString(wordBreak2.wordBreak("catsanddog", new HashSet<String>(Arrays.asList(dicStr)))));
}
}

leetcode — word-break-ii的更多相关文章

  1. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  2. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  3. [LeetCode] Word Break II 拆分词句之二

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

  4. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  5. [LeetCode] Word Break II 解题思路

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

  6. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  7. [Leetcode] word break ii拆分词语

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

  8. [LeetCode] Word Break II (TLE)

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

  9. LeetCode: Word Break II [140]

    [题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...

  10. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

随机推荐

  1. centos 自带mysql卸载时出现无法卸载情况

    [dianyi@localhost ~]$ rpm -qa|grep mysql mysql-libs-5.1.52-1.el6_0.1.x86_64 [dianyi@localhost ~]$ rp ...

  2. mysql优化sql语句的方法

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...

  3. 浏览器正确理解和使用GBK及UTF-8(UTF-8 + BOM)网页编码

    网页编码英文译为web page encoding.是在网页中指定其特定的字符编码格式的库. GBK是国家标准GB2312基础上扩容后兼容GB2312的标准. GBK的文字编码是用双字节来表示的.即不 ...

  4. 王磊:AI 时代物流行业的 OCR 应用

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ OCR 是人工智能里面非常重要的基础能力之一.腾讯云人工智能产品总监王磊,结合物流场景解读了OCR技术."OCR文本识别能够优化 ...

  5. Activiti 基本操作之“受理人变量”

    在 Activiti 流程引擎中,尽管通过 setAssignee(taskId, userId) 可以设置受理人,但这毕竟要先把下一步的任务查出来才能设置,比较繁琐:借助 Activiti 的 ac ...

  6. 利用反射操作bean的属性和方法

    今天在开发中碰到这样一个场景:当请求添加项目下的目录时,传过来的是一个IndexModel,这个Model里有关于这个目录字段的详细信息,包括基础报表,实时,漏斗等信息(这些字段类型都是boolean ...

  7. springboot+jpa+thymeleaf增删改查的示例(转)

    这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个demo来试试它的效果,越简单越容易上 ...

  8. Can't update: no tracked branch No tracked branch configured for branch dev.

    1.git pull 命令出现以下错误 $ git pull There is no tracking information for the current branch. Please speci ...

  9. SLAM入门之视觉里程计(2):相机模型(内参数,外参数)

    相机成像的过程实际是将真实的三维空间中的三维点映射到成像平面(二维空间)过程,可以简单的使用小孔成像模型来描述该过程,以了解成像过程中三维空间到二位图像空间的变换过程. 本文包含两部分内容,首先介绍小 ...

  10. Spark术语

    1.resilient distributed dataset (RDD) The core programming abstraction in Spark, consisting of a fau ...