LeetCode Word Break II
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相关推荐
- [LeetCode] Word Break II 拆分词句之二
[LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- 【DFS + 记忆化递归】LeetCode 140. Word Break II
LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...
- Leetcode word break I II 词句拆分I和II的java实现及解析
Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...
- Word Break Word Break II
leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...
- LeetCode——Word Break
LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...
- leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)
题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...
- 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 ...
- LeetCode() Word Search II
超时,用了tire也不行,需要再改. class Solution {class TrieNode { public:// Initialize your data structure here.Tr ...
最新文章
- 你的AI模型有哪些安全问题,在这份AI攻防”词典”里都能查到
- C++里如何使用功能键(F1、F2·····)进行操纵?
- 2017 ACM/ICPC Asia Regional Xian Online 记录
- Elasticsearch Curator使用
- 判断IE中某个ActiveX控件是否已经安装
- 租赁笔记本电脑哪里有_广东哪里有离心鼓风机优势-长沙鼓风机厂
- strlen函数实现
- Python实现“按任意键返回”和无回显输入
- php三年经验 多少工资_PHP2年以上经验,在深圳工资能拿多少?
- [转] Android SDK manager 无法获取更新版本列表
- ORB-SLAM2双目开源框架 (2) Tracking解析
- Struts2框架的概念及使用方法
- 操作系统(02326)课后习题答案
- 用Python自制一个百度一下,这操作可还行
- kali Linux桌面环境切换
- 11.什么是Heuristic
- 专利与论文-1:为什么要写专利?专利有什么好处?
- 小李飞刀:Python我又来啦,例无虚发~
- 通过PowerShell管理Office 365组
- 10大H5前端框架(转)