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. Vue单页面骨架屏实践

    github 地址: VV-UI/VV-UI 演示地址: vv-ui 文档地址:skeleton 关于骨架屏介绍 骨架屏的作用主要是在网络请求较慢时,提供基础占位,当数据加载完成,恢复数据展示.这样给 ...

  2. swiper3插件无缝滚动配置

    <html> <head> <link rel="stylesheet" href="https://cdn.bootcss.com/Swi ...

  3. springMVC使用jsp:include嵌入页面的两种方式

    1.静态嵌入子页面 <%@ include file="header.jsp" %>   静态嵌入支持 jsp . html . xml 以及纯文本. 静态嵌入在编译时 ...

  4. NIO相关基础篇二

    转载请注明原创出处,谢谢! 上篇NIO相关基础篇一,主要介绍了一些基本的概念以及缓冲区(Buffer)和通道(Channel),本篇继续NIO相关话题内容,主要就是文件锁.以及比较关键的Selecto ...

  5. Python 学习(1) 简单的小爬虫

    最近抽空学了两天的Python,基础知识都看完了,正好想申请个联通日租卡,就花了2小时写了个小爬虫,爬一下联通日租卡的申请页面,看有没有好记一点的手机号~   人工挑眼都挑花了. 用的IDE是PyCh ...

  6. 使用boost::multi_index高速构建排行榜

    使用boost::multi_index高速构建排行榜 前几天在boost的maillist上看到boost::multi_index将要支持ranked_index(邮件内容见附件2),这实乃我等苦 ...

  7. JSP具体篇——response对象

    response对象 response对象用于响应client请求,向客户输出信息. 他封装了JSP产生的响应,并发送到client以响应client请求. 1.重定向网页 使用response对象的 ...

  8. make工程管理器

    1.概述 大型程序中,人们希望工具自动识别修改的文件,而且不需要输入冗长的命令,就可以进行编译链接等操作,于是make工程管理器应运而生. make可以自动识别文件时间戳,只处理修改的文件: make ...

  9. ios应用版本号设置规则

    版本号的格式:v<主版本号>.<副版本号>.<发布号> 版本号的初始值:v1.0.0 管理规则: 主版本号(Major version) 1.  产品的主体构件进行 ...

  10. 腾讯WeTest《2017中国移动游戏质量白皮书》开放预约,再为国内手游把把脉

    产品为王,质量先行.如果说2016年是爆款手游相继崛起的一年,那么2017年则更像是打磨精品.建立生态的高手切磋之年.守住一个游戏的质量生命线,方能建立健康生态,方能在如火如荼的行业竞争中角逐到最后. ...