题目

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. CSUOJ 1040 Round-number

    Description Most of the time when rounding a given number, it is customary to round to some multiple ...

  2. python使用web.py开发httpserver,解决post请求跨域问题

    使用web.py做http server开发时,遇到postman能够正常请求到数据,但是浏览器无法请求到数据,查原因之后发现是跨域请求的问题. 跨域请求,就是在浏览器窗口中,和某个服务端通过某个 “ ...

  3. 线性表之顺序表C++实现

    线性表之顺序表 一.头文件:SeqList.h //顺序线性表的头文件 #include<iostream> ; //定义顺序表SeqList的模板类 template<class ...

  4. 【2005-2006 ACM-ICPC, NEERC, Moscow Subregional Contest】Problem J. Jack-pot

    简单dfs,差分一下A数组和建出字典树能写得更方便,若不这么做代码时就会像我一样难受. #include<cstdio> #include<cstring> #include& ...

  5. [HDU6198]number number number

    题目大意: 给定一个数k表示你可以从包括0的斐波那契数列中任取k个数,得到它们的和.求最小的不能得到的自然数. 思路: 打表找规律,可以发现答案为f(2k+3)-1,然后用公式f(i)=f(i/2)* ...

  6. 鸟哥的私房菜:Bash shell(五)-数据流重导向

    数据流重定向 数据流重导向就是将某个指令执行后应该要出现在屏幕上的数据, 给他传输到其它的地方,例如档案或者是装置 (例如打印机之类的!)!这玩意儿在 Linux 的文字模式底下可重要的! 尤其是如果 ...

  7. Elasticsearch 横向扩容以及容错机制

    写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...

  8. 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)完美支持。

    WAMPServer可以让开发者在Windows系统下快速搭建WAMP环境,它支持多版本的Apache.MySQL.PHP之间的相互切换,互不影响,对于PHPer开发者来讲极为方便快速. 以下是在WA ...

  9. ServletActionContext.getRequest().getSession() 和 ActionContext.getContext().getSession()

    ActionContext.getContext().getSession(); 这个方法获取的session是struts封装过的一个Map类型的session,只能调用put()方法缓存数据. S ...

  10. WindowsAPI详解——GetDriveType 获得逻辑驱动器的类型

    http://flyxxtt.blogbus.com/logs/43181576.html 函数原型:UINT GetDriveType(LPCTSTR lpRootPathName) 参数lpRoo ...