Skip to content

Commit 65cc3e2

Browse files
committed
单词切分2
1 parent 8c067c9 commit 65cc3e2

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

LeetCode/Doc/单词切分2.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# word-break-ii(单词切分2)
2+
3+
<center>知识点:动态规划</center>
4+
5+
6+
## 题目描述
7+
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.
8+
9+
Return all such possible sentences.
10+
11+
For example, given
12+
s ="catsanddog",
13+
dict =["cat", "cats", "and", "sand", "dog"].
14+
15+
A solution is["cats and dog", "cat sand dog"].
16+
17+
-----
18+
19+
给定字符串s和单词字典dict,在s中添加空格以构造一个句子,其中每个单词都是有效的字典单词。
20+
21+
返回所有这些可能的句子。
22+
23+
比如:
24+
s ="catsanddog",
25+
dict =["cat", "cats", "and", "sand", "dog"].
26+
27+
应该返回["cats and dog", "cat sand dog"].
28+
29+
## 解题思路
30+
31+
我们从某一个位置index将s切分为前后两个部分,分别记为s1和s2,如果s1在dict中,我们去计算s2计算的结果,然后将s2切分的结果与s1合并即可,如果s2不能切割,我们就从index+1继续对s进行切割,实际是一种dfs的思想,核心思想如下:
32+
33+
```java
34+
ArrayList<String> wordBreak(String s, Set<String> dict){
35+
ArrayList<String> res;
36+
for(int index=1;index<s.length;index++){
37+
if(dict.contains(s[0:index])){
38+
ArrayList<String> list=wordBreak(s[index:],dict);
39+
if(list==null){
40+
continue;
41+
}else{
42+
res+=merge(s[0:index],list);
43+
}
44+
}
45+
}
46+
return res;
47+
}
48+
```
49+
50+
这样虽然可以解决问题,但是有点小瑕疵,考虑:s="abcdefg",dict={"bc","def","g"},那么:
51+
52+
- index=1 s1=a s2=bcdefg ,切割bcdefg
53+
- Index=2 s1=ab s2=cdefg,切割cdefg
54+
- index=3 s1=abc s2=defg,切割defg
55+
- ...
56+
57+
注意,第一步切割bcdefg的时候递归调用了wordBreak,在切割bcdefg的时候也会切割cdefg、defg、efg、fg、g,然后下面几步也是类似的操作,所以说下面几步实际上做了一部分重复的工作,为了避免这种重复的工作,我们可以使用Map,将s与其切分的结果存下来,这样下次再遇到s的时候直接从map中取之前分好的结果就行了,不用做重复操作。
58+
59+
想上面那样的思路写出来的代码和牛客网评测系统给出的字符串的顺序不一样,观察了一下刚好和上面的顺序是反的,只要把从前到后遍历改成从后到前遍历即可。
60+
61+
62+
## 代码
63+
64+
[这里](../src/twelve/Solution.java)
Binary file not shown.
Binary file not shown.

LeetCode/src/eleven/Solution.java

+2
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ public static void main(String[] args) {
4141
System.out.println(solution.wordBreak("aaaaaaa", dict));
4242
}
4343
}
44+
45+

LeetCode/src/twelve/Solution.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package twelve;
2+
3+
/**
4+
* @author dmrfcoder
5+
* @date 2019-04-18
6+
*/
7+
8+
import java.util.*;
9+
10+
public class Solution {
11+
public ArrayList<String> wordBreak(String s, Set<String> dict) {
12+
ArrayList<String> dfs = dfs(s, dict, new HashMap<>());
13+
14+
return dfs;
15+
}
16+
17+
private ArrayList<String> dfs(String s, Set<String> dict, Map<String, ArrayList<String>> map) {
18+
19+
ArrayList<String> arrayList = new ArrayList<>();
20+
if ("".equals(s)) {
21+
arrayList.add("");
22+
return arrayList;
23+
}
24+
if (map.containsKey(s)) {
25+
return map.get(s);
26+
} else {
27+
for (int index = s.length()-1; index >= 0; index--) {
28+
if (dict.contains(s.substring(index))) {
29+
ArrayList<String> dfsResult = dfs(s.substring(0,index), dict, map);
30+
if (dfsResult.size() == 0) {
31+
continue;
32+
} else {
33+
String temp = s.substring(index);
34+
35+
for (String str : dfsResult) {
36+
if ("".equals(str)) {
37+
arrayList.add(temp);
38+
} else {
39+
StringBuilder stringBuilder = new StringBuilder();
40+
stringBuilder.append(str).append(" ").append(temp);
41+
arrayList.add(stringBuilder.toString());
42+
}
43+
44+
}
45+
46+
}
47+
}
48+
}
49+
}
50+
map.put(s, arrayList);
51+
return arrayList;
52+
53+
}
54+
55+
public static void main(String[] args) {
56+
Set<String> dict = new HashSet<>();
57+
dict.add("cat");
58+
dict.add("cats");
59+
dict.add("and");
60+
dict.add("sand");
61+
dict.add("dog");
62+
dict.add("aaaa");
63+
dict.add("aa");
64+
dict.add("a");
65+
dict.add("b");
66+
Solution solution = new Solution();
67+
ArrayList<String> list = solution.wordBreak("catsanddog", dict);
68+
for (String s : list) {
69+
System.out.println(s);
70+
}
71+
72+
}
73+
74+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,5 @@
153153
- [linked-list-cycle-ii(找出链表中环的入口节点)](./LeetCode/Doc/找出链表中环的入口节点.md)
154154
- [linked-list-cycle(判断链表中是否存在环)](./LeetCode/Doc/判断链表中是否存在环.md)
155155
- [word-break(单词切分)](./LeetCode/Doc/单词切分.md)
156+
- [word-break-ii(单词切分2)](./LeetCode/Doc/单词切分2.md)
156157

0 commit comments

Comments
 (0)