Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

解法1,递归,超时

有两种递归方式,一种是按s的子串递归,一种是按集合dict递归

第一种:

 public boolean wordBreak(String s, Set<String> dict) {
if(dict.contains(s)){
return true;
}
boolean flag = false;
for(int i=1;i<s.length();i++){
if(dict.contains(s.substring(0,i))){
flag = wordBreak(s.substring(i),dict);
}
}
return flag;
}

第二种:

 public boolean wordBreak(String s, Set<String> dict) {
if(dict.contains(s)||s.equals("")){
return true;
}
boolean flag = false;
for(String now:dict){
for(int i = 0;i<s.length()-now.length();i++){
if(s.substring(i, i+now.length()).equals(now)){
flag = wordBreak(s.substring(0,i), dict)&&wordBreak(s.substring(i+now.length()), dict);
if(flag){
return true;
}
}
}
}
return flag;
}

解法2:动态规划

设flag[]为boolean数组,flag[i]表示s.substring(0,i)是否满足wordbreak,因此有状态转移方程为:

flag[n] = ∑flag[i]&&s.substring(i,n)   (i 取值为从1到n-1),有代码如下:

 public boolean wordBreak(String s, Set<String> dict) {
boolean flag[] = new boolean[s.length()+1];
flag[0] = true;
for(int i=1;i<=s.length();i++){
for(int j=0;j<i;j++){
flag[i] = flag[i] || (flag[j]&&dict.contains(s.substring(j, i)));
if(flag[i]){
continue;
}
}
}
return flag[s.length()];
}

递归的复杂度为O(2^n),动态规划的复杂度为O(n^2)

个人感觉,动态规划版本就是递归中第一种解法的非递归版,递归是自顶向下计算,动态规划是自底向上计算。

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

做法与上面相似,boolean数组变成list<Integer>的数组,记录当前位可以从前面哪些位得出,先自底向上生成数组,然后自顶向下构造字符串,直接贴代码:

 private List<String> result = new ArrayList<String>();
public List<String> wordBreak(String s, Set<String> dict) {
List<List<Integer>> flag = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
temp.add(0);
flag.add(temp);
for (int i = 1; i <= s.length(); i++) {
temp = new ArrayList<Integer>();
for (int j = 0; j < i; j++) {
if (dict.contains(s.substring(j, i)) && flag.get(j).size() > 0) {
temp.add(j);
}
}
flag.add(temp);
}
genList(flag, s, "", flag.get(flag.size() - 1), flag.size() - 1);
return result;
} private void genList(List<List<Integer>> flag, String source, String now,
List<Integer> list, int pos) {
// TODO Auto-generated method stub
if (pos == 0) {
result.add(now);
return;
}
for (Integer i : list) {
String temp = "";
if (now.equals("")) {
temp = source.substring(i, pos); } else {
temp = source.substring(i, pos) + " " + now;
}
genList(flag, source, temp, flag.get(i), i.intValue());
}
}

Word Break I II的更多相关文章

  1. LeetCode: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

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

  3. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  4. LeetCode:Word Break II(DP)

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

  5. 140. Word Break II

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

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

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

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

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

  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. soj 1700 ping_简单dp

    题目链接 题意:给你一个无向图,求n边的最短路 思路:用最短路想了半天都没想出来,比赛结束回去看看原来用dp做,我的dp有待提高啊 sp[i][k]=min(sp[j][k-1]+dp[j][i])/ ...

  2. Java宝典(四)------Java中也存在内存泄露。

    --Java中会存在内存泄露吗? --如果你想当然的以为Java里有了垃圾回收机制就不会存在内存泄露,那你就错了. Java里也会存在内存泄露! 我们慢慢来分析. 所谓内存泄露就是指一个不再被程序使用 ...

  3. poj 2229 Sumsets DP

    题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...

  4. 动态规划之最长公共子序列LCS(Longest Common Subsequence)

    一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...

  5. 精通CSS+DIV基础总结(二)

    上一篇我们已经总结了部分CSS+DIV相关知识,这篇我们接着总结,从下边几个方面学习一下: 一,我们看如何设置网页的背景,顾名思义背景可以通过颜色和图片来设置,下边我们看一下如何设置: 颜色的设置非常 ...

  6. 国内ip信息库的组建

    1.从 APNIC 分析得到国内的段 数据源位置:http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest 2.从QQ纯真库分析得到国 ...

  7. Oracle-nomount/mount/open

    通常所说的Oracle Server主要由两个部分组成:Instance和Database.Instance是指一组后台进程(在Windows上是一组线程)和一块共享内存区域:Database是指存储 ...

  8. python- 迭代器与生成器

    1.迭代器: 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么, 因为人们很少在迭代途中往后退.另外,迭代器的一 ...

  9. .NET基础拾遗(1)类型语法基础和内存管理基础1

    一.基础类型和语法 1.1 .NET中所有类型的基类是什么? 在.NET中所有的内建类型都继承自System.Object类型. 1.2 值类型和引用类型的区别? 在.NET中的类型分为值类型和引用类 ...

  10. HTML 表单与输出

    我们先来设置一个简单的表单 <!doctype html><html><head> <meta charset="utf-8"> & ...