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. 【CodeForces 606A】A -特别水的题1-Magic Spheres

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/A Description Carl is a beginne ...

  2. 【UVA 1451】Average

    题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...

  3. 利用Spring中的HtmlUtils.htmlEscape(input)过滤html

    fatherModule.setModuelName(HtmlUtils.htmlEscape(fatherModule.getModuelName())); log.info(HtmlUtils.h ...

  4. Vijos1901 学姐的钱包

    描述 学姐每次出门逛街都要带恰好M元钱, 不过她今天却忘记带钱包了.可怜的doc只好自己凑钱给学姐, 但是他口袋里只有一元钱.好在doc的N位朋友们都特别有钱, 他们答应与doc作一些交换.其中第i位 ...

  5. HDU 5497 Inversion

    Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description You have ...

  6. node相关的精典材料

    node.js电子书 了不起的Node.js 深入浅出Node.js node.js入门经典 node.js开发指南 node.js相关优秀博文 官网 Infoq深入浅出Node.js系列(进阶必读) ...

  7. form的submit与onsubmit的用法与区别

    发生顺序:onsubmit -> submit1.阻止表单提单:<script>function submitFun(){    //逻辑判断    return true; //允 ...

  8. 关于Asp.Net Mvc3.0 使用KindEditor4.0 上传图片与文件

    http://blog.csdn.net/fyxq14hao/article/details/7245502 今天我们的Asp.Net Mvc 3的项目中,把KindEditor3.9改为 KindE ...

  9. The AndroidManifest.xml File

    manifest (船运的)载货清单 http://www.android-doc.com/guide/topics/manifest/manifest-intro.html Every applic ...

  10. Protocol Buffer技术详解(数据编码)

    Protocol Buffer技术详解(数据编码) 之前已经发了三篇有关Protocol Buffer的技术博客,其中第一篇介绍了Protocol Buffer的语言规范,而后两篇则分别基于C++和J ...