题目

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. 《JAVA软件结构与数据结构》第一周学习总结

    学号 20172326 <JAVA软件结构与数据结构>第一周学习总结 教材学习内容总结 软件质量的几大特性 增长函数与大O记法 大O记法用来表示表示增长函数,从而来表示算法的复杂度 算法的 ...

  2. 洛谷——P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  3. 洛谷——P1231 教辅的组成

    P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...

  4. interrupt_control

    中断的概念CPU在处理过程中,经常需要同外部设备进行交互,交互的方式由“轮询方式”“中断方式” 轮询方式: 方式:在同外设进行交互的过程中,CPU每隔一定的时间状态就去查询相关的状态位,所以在交互期间 ...

  5. django信号调度的用法

    Django中提供了"信号调度",用于在框架执行操作时解耦. 一些动作发生的时候,系统会根据信号定义的函数执行相应的操作 Django中内置的signal Model_signal ...

  6. [LOJ2542][PKUWC2018]随机游走(MinMax容斥+树形DP)

    MinMax容斥将问题转化为求x到S中任意点的最小时间. 树形DP,直接求概率比较困难,考虑只求系数.最后由于x节点作为树根无父亲,所以求出的第二个系数就是答案. https://blog.csdn. ...

  7. 【20181031T1】一串数字【分解质因数+贪心】

    题面 [错解] 立方就是所有质因子次数都是3的倍数嘛 发现1e5的三次根很小,可以枚举所有和这个数乘起来是完全立方数的(flag*1) 然后--连条边跑最大独立集? 不对啊是NP问题(实际上是个二分图 ...

  8. Python编码规则

    1. 命名规则 1.1 变量名.包名.模块名 变量名通常有字母.数字和下划线组成,且首字母必须是字母或下划线,并且不能使用python的保留字:包名.模块名通常用小写字母 1.2 类名.对象名 类名首 ...

  9. JQ中get()与eq()的区别

    .eq() : 减少匹配元素的集合,根据index索引值,精确指定索引对象. .get() : 通过检索匹配jQuery对象得到对应的DOM元素. 同样是返回元素,那么eq与get有什么区别呢? eq ...

  10. JavaEE-学习目录

    JavaEE ============================================ Web工作机制 JSP Struts基础 Struts核心文件 Struts数据校验与国际化 S ...