题目

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

题解

这道题不仅仅是看是不是wordbreak,还需要在此基础上把所有word break的结果保存。

为了把所有可能性都保存,那么就使用DFS方法来解决。DFS主要就是跳的层次不容易看出,我下面就以字符串leetcode字典le l et eet code作为例子画了一张图,大概讲解了如何递回和返回,这样更加有助于理解。

代码如下:

 1     public boolean wordBreakcheck(String s, Set<String> dict) {
 2         if(s==null || s.length()==0)
 3             return true;
 4         boolean[] res = new boolean[s.length()+1];
 5         res[0] = true;
 6         for(int i=0;i<s.length();i++){
 7             StringBuilder str = new StringBuilder(s.substring(0,i+1));
 8             for(int j=0;j<=i;j++){
 9                 if(res[j] && dict.contains(str.toString())){
                     res[i+1] = true;
                     break;
                 }
                 str.deleteCharAt(0);
             }
         }
         return res[s.length()];
     }
     
     public ArrayList<String> wordBreak(String s, Set<String> dict) {  
         ArrayList<String> res = new ArrayList<String>();  
         if(s==null || s.length()==0)  
             return res;
         if(wordBreakcheck(s,dict))
             helper(s,dict,0,"",res);  
         return res;  
     }  
     private void helper(String s, Set<String> dict, int start, String item, ArrayList<String> res){  
         if(start>=s.length()){  
             res.add(item);  
             return;  
         }
         
         StringBuilder str = new StringBuilder();  
         for(int i=start;i<s.length();i++){  
             str.append(s.charAt(i));  
             if(dict.contains(str.toString())){  
                 String newItem = new String();  
                 if(item.length()>0)
                     newItem = item + " " + str.toString();
                 else
                     newItem = str.toString();
                 helper(s,dict,i+1,newItem,res);  
             }  
         }  
     }  

Reference: http://blog.csdn.net/linhuanmars/article/details/22452163

Word Break II leetcode java的更多相关文章

  1. Word Break II——LeetCode

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

  2. Word Ladder II leetcode java

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

  3. Word Break II -- leetcode

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

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

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

  6. [Leetcode Week9]Word Break II

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

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

  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 139. Word Break 、140. Word Break II

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

随机推荐

  1. JDK源码分析(三)——HashMap 上(基于JDK7)

    目录 HashMap概述 内部字段及构造方法 存储元素 扩容 取出元素 删除元素 判断 总结 HashMap概述   前面我们分析了基于数组实现的ArrayList和基于双向链表实现的LinkedLi ...

  2. SpringMvc和servlet对比

    一.servlet实现登录. 咱们先来看一下servlet实现注册登录. <servlet> <servlet-name>LoginServlet</servlet-na ...

  3. 总结html

    1.初识html W3C : 万维网联盟!(World Wide Web Consortium )   创建于1994年,是web技术领域最权威最具有影响力的标准机构!           W3C规定 ...

  4. BZOJ2673 [Wf2011]Chips Challenge 费用流 zkw费用流 网络流

    https://darkbzoj.cf/problem/2673 有一个芯片,芯片上有N*N(1≤N≤40)个插槽,可以在里面装零件. 有些插槽不能装零件,有些插槽必须装零件,剩下的插槽随意. 要求装 ...

  5. hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树

    题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 #include<cstdio& ...

  6. 【ACM-ICPC 2018 沈阳赛区网络预赛】不太敢自称官方的出题人题解

    A. Gudako and Ritsuka 链接 by Yuki & Asm.Def 期望难度:Hard- 考虑从后往前进行博弈动态规划,在这一过程中维护所有的先手必胜区间.区间不妨采用左开右 ...

  7. Html的学习随笔

    在<head>的<style>中定义样式,有#号,比如#header就是定义一种名为header的样式,后面用id=header来调用:而无#号,比如直接就是header,那后 ...

  8. BZOJ 4521 CQOI 2016 手机号码 数位DP

    4521: [Cqoi2016]手机号码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 539  Solved: 325[Submit][Status ...

  9. react-native开发总结

    项目地址:http://liu12fei08fei.github.io/blog/41react-native.html 说明 • 项目总结代码地址 • 从项目开始启动(2018-07-02)到项目进 ...

  10. 1. python 字符串简介与常用函数

    1. python中的字符串简介与常用函数 在python中,字符串变成了一个强大的处理工具集,他是不可变的,也就是说字符串包含字符与字符的顺序,他不可以原处修改 字符串是我们后面需要学习的稍大一点的 ...