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"].

解题思路:

参考上题思路二的解法,修改下代码,让其显示路径即可,JAVA实现如下:

static public List<String> wordBreak(String s, Set<String> wordDict) {
List<String> list = new ArrayList<String>();
dfs(list, s, wordDict, new HashSet<String>(), new ArrayList<String>());
return list;
} static public boolean dfs(List<String> list, String s, Set<String> dict,
Set<String> unmatch, List<String> alist) {
boolean isMatch=false;
for (String prefix : dict) {
if (s.equals(prefix)) {
alist.add(prefix);
StringBuilder sb = new StringBuilder();
for (String str : alist) {
sb.append(str);
sb.append(" ");
}
list.add(sb.substring(0, sb.length() - 1));
alist.remove(alist.size() - 1);
isMatch=true;
continue;
} else if (s.startsWith(prefix)) {
String suffix = s.substring(prefix.length());
if (!unmatch.contains(suffix)) {
alist.add(prefix);
if (!dfs(list, suffix, dict, unmatch, alist))
unmatch.add(suffix);
else isMatch=true;
alist.remove(alist.size() - 1);
}
}
}
return isMatch;
}

Java for LeetCode 140 Word Break II的更多相关文章

  1. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  2. leetcode 140. Word Break II ----- java

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

  3. Leetcode#140 Word Break II

    原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...

  4. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  5. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  6. 【LeetCode】140. Word Break II

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

  7. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  8. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. 【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 ...

随机推荐

  1. HTTP各个状态返回值

    转载来自于:http://desert3.iteye.com/blog/1136548 502 Bad Gateway:tomcat没有启动起来 504 Gateway Time-out: nginx ...

  2. UTL_FILE详解

    包UTL_FILE 提供了在操作系统层面上对文件系统中文件的读写功能.非超级用户在使用包UTL_FILE中任何函数或存储过程前必须由超级用户授予在这个包上的EXECUTE权限.例如:我们使用下列命令对 ...

  3. 写在SDOI2016Round1前的To Do List

    理性的整理了一下自己的不足. 计算几何啥都不会,字符串类DP毫无练习,数据结构写的不熟,数论推不出式子,网络流建模常建残: 需要达成的任务: 一.网络流: 熟练网络流的板子(之前一直仰慕zkw费用流, ...

  4. IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)

    目录 . 引言 . IIS 6.0 FTP匿名登录.匿名可写加固 . IIS 7.0 FTP匿名登录.匿名可写加固 . IIS >= 7.5 FTP匿名登录.匿名可写加固 . IIS 6.0 A ...

  5. myeclipse 引入jar包 (包括 jdbc 驱动引用)

    A.直接用MyEclipse里自带的相关的项目jar包,右击项目"MyEclipse"菜单,选择对应的jar包就OK了 B.添加外部的jar包到web项目的lib包下,右击项目&q ...

  6. iOS应用支持IPV6

    一.IPV6-Only支持是啥? 首先IPV6,是对IPV4地址空间的扩充.目前当我们用iOS设备连接上Wifi.4G.3G等网络时,设备被分配的地址均是IPV4地址,但是随着运营商和企业逐渐部署IP ...

  7. POJ1836Alignment(LCA)

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15135   Accepted: 4911 Descri ...

  8. File类的创建,删除文件

    File.Create(@"C:\Users\shuai\Desktop\new.txt"); Console.WriteLine("创建成功"); Conso ...

  9. inux环境PHP7.0安装

    inux环境PHP7.0安装   PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导 ...

  10. CSS3 动画animation

    关键帧 什么是关键帧.一如上面对Flash原理的描述一样,我们知道动画其实由许多静态画面组成,第一个这样的静态画面可以表述为一帧.其中关键帧是在动画过程中体现了物理明显变化的那些帧. 比如之前的例子中 ...