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”]. 
思路分析:这题可以用DFS 搜索递归做,基本是brute force的解法,dfs函数要维护的量包括startIndex,preWords和res,startIndex表示当前进行word break的起点,也就是说前面如果已经被break了,应该排除在外。preWords主要维护目前已经breaked的string前面的部分,后面从startIndex一旦发现新的word可以添加到preWords之后,dfs返回的条件是当startIndex已经越界,也就是>=s.length()了,就把当前得到的word break方案加入res。从题目的实例我们也可以看出,同一个字符串可以有多个word break方案,因此我们从前向后scan 字符串发现第一个可以break的词的时候需要更新preWords进行dfs递归调用。这题是NP问题,时间复杂度为O(2^n)也就是指数级别。 这类DFS递归搜索的题目有很多,除了word break,还有八皇后,Sudoku Solver等等,都是这类题目,在面试中很常见,要多加练习。 
AC Code:以下是brute force DFS搜索AC的从code。由于LeetCode有一个很长的不能break的测试用例,因此把word break I 判断能否break的函数作为sub routine,先判断一下能否break,如果不能直接返回空容器。

public class Solution {public List<String> wordBreak(String s, Set<String> dict) {ArrayList<String> res = new ArrayList<String>();if(s == null || s.isEmpty() || !wordBreakCanDo(s, dict)){return res;}dfs(s, dict, 0, "", res);return res;}//10:08public void dfs(String s, Set<String> dict, int startIndex, String preWords, ArrayList<String> res){if(startIndex >= s.length()){//return contition is that the startIndex has been out of boundres.add(preWords);return;}for(int i = startIndex; i < s.length(); i++){String curStr = s.substring(startIndex, i+1);if(dict.contains(curStr)){String newSol;if(preWords.length() > 0){newSol = preWords + " " + curStr;} else {newSol = curStr;}dfs(s, dict, i + 1, newSol, res);}}}//1021public boolean wordBreakCanDo(String s, Set<String> dict) {s = "#" + s;boolean[] canSegmented = new boolean[s.length()];canSegmented[0] = true;for(int i = 1; i < s.length(); i++){for(int k = 0; k < i; k++){canSegmented[i] = canSegmented[k] && dict.contains(s.substring(k + 1, i + 1));if(canSegmented[i]) break;}}return canSegmented[s.length() - 1];}
}

LeetCode Word Break II相关推荐

  1. [LeetCode] Word Break II 拆分词句之二

    [LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...

  2. LeetCode: Word Break II [140]

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

  3. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  4. Leetcode word break I II 词句拆分I和II的java实现及解析

    Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...

  5. Word Break Word Break II

    leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...

  6. LeetCode——Word Break

    LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...

  7. leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)

    题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...

  8. leetcode word break java,Word Break leetcode java

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  9. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution {class TrieNode { public:// Initialize your data structure here.Tr ...

最新文章

  1. 你的AI模型有哪些安全问题,在这份AI攻防”词典”里都能查到
  2. C++里如何使用功能键(F1、F2·····)进行操纵?
  3. 2017 ACM/ICPC Asia Regional Xian Online 记录
  4. Elasticsearch Curator使用
  5. 判断IE中某个ActiveX控件是否已经安装
  6. 租赁笔记本电脑哪里有_广东哪里有离心鼓风机优势-长沙鼓风机厂
  7. strlen函数实现
  8. Python实现“按任意键返回”和无回显输入
  9. php三年经验 多少工资_PHP2年以上经验,在深圳工资能拿多少?
  10. [转] Android SDK manager 无法获取更新版本列表
  11. ORB-SLAM2双目开源框架 (2) Tracking解析
  12. Struts2框架的概念及使用方法
  13. 操作系统(02326)课后习题答案
  14. 用Python自制一个百度一下,这操作可还行
  15. kali Linux桌面环境切换
  16. 11.什么是Heuristic
  17. 专利与论文-1:为什么要写专利?专利有什么好处?
  18. 小李飞刀:Python我又来啦,例无虚发~
  19. 通过PowerShell管理Office 365组
  20. 10大H5前端框架(转)

热门文章

  1. 拍拍贷用户及还款数据分析案例
  2. 画个板子玩一玩STM32F030F4P6,也许是最便宜的32bit MCU
  3. vue简单的图片相册幻灯片实例
  4. win7计算机没有光驱图标不见了,关于如何解决Win7光驱图标消失的问题的讲解
  5. Python小练习 - 判断是否为“回联文”
  6. 啄木鸟Python社区
  7. StrongShop 开源商城 跨境电商独立站的理想选择
  8. Docker入门笔记(七)——镜像
  9. 应急通信指挥调度系统|智慧消防指挥中心
  10. 个人信息泄露 大病难治