【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"].



题解:开始想利用Word Break I的代码(详细请见http://www.cnblogs.com/sunshineatnoon/p/3849154.html),先把dp的表打出来,然后DFS搜索出字符串,后来发现这样会TLE。看了九章的题解,才有了如下思路:

递归的思想,利用一个map——Map<String, ArrayList<String>> map存放单词s和它对应的拆分字符串的列表。比如题目中的例子catsanddog在map中的对应的list就有两个元素"cats and dog"和"cat sand dog"。

因为每个字符串s如果有划分,那么一定从第一个字符开始,所以,递归每次从索引0开始,找到dict中包含的s的前缀,然后递归的探求s除去这个前缀后剩下的字符串可以被拆分成的方式,如果剩下的字符串可以被拆分,那么前缀加上剩下字符串拆分得到的结果就是最终s的拆分结果了。

还是以题目中的例子来说"catsanddog"

首先发现dict中含有前缀cat,这时递归的去找"sanddog"的拆分方式,发现可以拆分为”sand dog",最终得到s的一种拆分方式"cat sand dog";

继续循环,发现dict中含有前缀"cats",这时递归的去找"anddog"的拆分方式,发现可以拆分为"and dog",最终得到s的第二种拆分方式"cats and dog"。

代码如下:

 1 public class Solution {
 2     public List<String> wordBreak(String s, Set<String> dict) {
 3         Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
 4         return findList(s, dict, map);
 5     }
 6     private List<String> findList(String s,Set<String> dict,Map<String, ArrayList<String>> map){
 7         if(map.containsKey(s)) return map.get(s);
 8         ArrayList<String> answerList = new ArrayList<String>();
 9         int length = s.length();
10         if(length <= 0)
11             return answerList;
12
13         for(int i = 1;i <= length;i++){
14             String prefix = s.substring(0,i);
15             if(dict.contains(prefix)){
16                 if(i == length)
17                     answerList.add(prefix);
18                 else{
19                     List<String> temp = findList(s.substring(i), dict, map);
20                     for(String tmp:temp){
21                         tmp = prefix + " " + tmp;
22                         answerList.add(tmp);
23                     }
24                 }
25             }
26         }
27         map.put(s, answerList);
28         return answerList;
29     }
30 }

【leetcode】Word Break II,布布扣,bubuko.com

时间: 2024-10-07 06:32:38

【leetcode】Word Break II的相关文章

【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&quo

【LeetCode】Word Break II 动态规划

题目:Word Break 要求找到所有能够有字典中的词重组成目标串的结果 public class Solution { public static List<String> wordBreak(String s, Set<String> dict) { List<String> dp[] = new ArrayList[s.length()+1]; dp[0] = new ArrayList<String>(); for(int i=0; i<s.

【leetcode】Word Break II (hard)★

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, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【leetcode】Word Break (middle)

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Word Break 动态规划

题目:Word Break 思路:将一个串可以划分的共有s.length+1个点,判断长为n的串是否能由字典中的词组成,则看之前有没有划分点能使其处于字典中 ,这样该问题 就分解为子问题的求解 所以可以使用动态规划 <span style="font-size:18px;">public class Solution { public boolean wordBreak(String s, Set<String> dict) { boolean[] tag =

【leetcode】Word Break(python)

思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符開始找连续的子串,可是这时与第一个就稍有不同.比方说word='ab', dict={ 'a', ab', ...},在找到a后,接下来处理的是b.我们发现b不在dict中,可是我们发现b能够和a结合,形成ab,而ab在dict中.所以这里的每一个子串就能够有三种选择.要么自己单独作为

【leetcode】Word Search II(hard)★

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le