题目

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.  1     public boolean wordBreakcheck(String s, Set<String> dict) {
  2.  2         if(s==null || s.length()==0)
  3.  3             return true;
  4.  4         boolean[] res = new boolean[s.length()+1];
  5.  5         res[0] = true;
  6.  6         for(int i=0;i<s.length();i++){
  7.  7             StringBuilder str = new StringBuilder(s.substring(0,i+1));
  8.  8             for(int j=0;j<=i;j++){
  9.  9                 if(res[j] && dict.contains(str.toString())){
  10.                      res[i+1] = true;
  11.                      break;
  12.                  }
  13.                  str.deleteCharAt(0);
  14.              }
  15.          }
  16.          return res[s.length()];
  17.      }
  18.      
  19.      public ArrayList<String> wordBreak(String s, Set<String> dict) {  
  20.          ArrayList<String> res = new ArrayList<String>();  
  21.          if(s==null || s.length()==0)  
  22.              return res;
  23.          if(wordBreakcheck(s,dict))
  24.              helper(s,dict,0,"",res);  
  25.          return res;  
  26.      }  
  27.      private void helper(String s, Set<String> dict, int start, String item, ArrayList<String> res){  
  28.          if(start>=s.length()){  
  29.              res.add(item);  
  30.              return;  
  31.          }
  32.          
  33.          StringBuilder str = new StringBuilder();  
  34.          for(int i=start;i<s.length();i++){  
  35.              str.append(s.charAt(i));  
  36.              if(dict.contains(str.toString())){  
  37.                  String newItem = new String();  
  38.                  if(item.length()>0)
  39.                      newItem = item + " " + str.toString();
  40.                  else
  41.                      newItem = str.toString();
  42.                  helper(s,dict,i+1,newItem,res);  
  43.              }  
  44.          }  
  45.      }  

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 1009 抛硬币

    Description James得到了一堆有趣的硬币,于是决定用这些硬币跟朋友们玩个小游戏.在一个N行M列的表格上,每一个第i行第j列的格子上都放有一枚James的硬币,抛该硬币正面朝上的概率为Pi ...

  2. [leetcode tree]103. Binary Tree Zigzag Level Order Traversal

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  3. Highmaps网页图表教程之下载Highmaps与Highmaps的地图类型

    Highmaps网页图表教程之下载Highmaps与Highmaps的地图类型 认识Highmaps Highmaps是Highcharts的姊妹框架,用来实现地图图表.它完全使用Javascript ...

  4. Python实现QQ自动点赞

    用Python做一个QQ自动点赞神器,上代码: 1 def QQZan(qq): 2 browser = webdriver.Chrome() 3 browser.maximize_window() ...

  5. thinkphp5使用redis

    1.设置应用配置文件config.php type可以是很多分类File.Redis等等 2.thinkphp5使用redis新建application/index/controller/index. ...

  6. 【BZOJ 3661】 Hungry Rabbit (贪心、优先队列)

    3661: Hungry Rabbit Time Limit: 100 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 67  Solved: 4 ...

  7. 递归与分治策略之棋盘覆盖Java实现

    递归与分治策略之棋盘覆盖 一.问题描述 二.过程详解 1.棋盘如下图,其中有一特殊方格:16*16 . 2.第一个分割结果:8*8 3.第二次分割结果:4*4 4.第三次分割结果:2*2 5.第四次分 ...

  8. python 中__name__ = '__main__' 的作用,到底干嘛的?

    python 中__name__ = 'main' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and execu ...

  9. Java 接口与抽象类

    抽象类 <JAVA编程思想>一书中,将抽象类定义为"包含抽象方法的类".只要用abstract修饰的类就是抽象类,抽象类不一定包含抽象方法,但有抽象方法的类一定是抽象类 ...

  10. 利用Everything开启http服务测试移动端浏览器环境

    一.Everything 简介 Everything本身是一款小巧的文件搜索神器,可以快速的搜索电脑中的文件,速度非常快. 二.使用Everything的http服务 在做移动端浏览器页面时,有时需要 ...