leetcode — word-break-ii
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的更多相关文章
- 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 ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [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 ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- 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 ...
随机推荐
- python源文件转换成exe问题解决贴
项目上做一个小工具,通过webservice接口实现配置下发.python文件调试通过了,想把它抓换成exe,网上查了下,得知有py2exe这个好用精简的小工具,本以为分分钟搞定的事情,结果经历了九转 ...
- lnmp1.3 配置pathinfo---thinkphp3.2 亲测有效
lnmp1.3环境下配置pathinfo模式试了很多方法,都以失败告终,博主被这个问题困扰了很久,终于解决了!现记录如下: 1.打开php.ini 通常该文件在 /usr/local/php/etc/ ...
- django序列化时使用外键的真实值
展示: 普通情况下序列化得到的外键的内容仅仅是id: ... { fields: { uat_date: "2015-07-25", statu: "CG", ...
- 杭电1513Palindrome
Palindrome Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 动态规划-迷宫-百度之星-Labyrinth
Labyrinth Problem Description 度度熊是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走 ...
- UVA 10465 Homer Simpson(全然背包: 二维目标条件)
UVA 10465 Homer Simpson(全然背包: 二维目标条件) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&a ...
- linux0.11学习笔记(1)
公布软件包包括内容: bootimage.Z - 具有美国键盘代码的压缩启动映像文件: rootimage.Z - 以1200kB 压缩的根文件系统映像文件: linux-0.11.tar.Z- 内核 ...
- LeetCode OJ 之 Ugly Number II (丑数-二)
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- Laravel技巧之记录多日志
相信每个小伙伴在使用laravel的时候都会记录日志.查看日志.那么问题来了,比如我在对接zabbix接口的时候,使用 Log::info() 会让日志全部记录在 storage/logs/larav ...
- MVC Anti-XSS方案
1:Form提交模式 在使用Form提交时,MVC框架提供了一个默认的机制.如果数据中含有恶意字,则会自动转向出错页面. 2:Ajax+JSON提交模式. MVC框架未提供对于Json数据的Ant ...