0%

Leetcode140-wordBreakII

Description

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example

Example 1:

1
2
3
4
5
6
7
8
Input:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
Output:
[
"cats and dog",
"cat sand dog"
]

Example 2:
1
2
3
4
5
6
7
8
9
10
Input:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
Output:
[
"pine apple pen apple",
"pineapple pen apple",
"pine applepen apple"
]
Explanation: Note that you are allowed to reuse a dictionary word.

Example 3:
1
2
3
4
5
Input:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
Output:
[]

Solution

Solution 1

DFS function returns an array including all substrings derived from s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution{
// DFS solution3: store useful substring
public List<String> wordBreak(String s, List<String> wordDict) {
HashSet<String> dic = new HashSet<>();
for (String word: wordDict) dic.add(word);
return DFS(s, dic, new HashMap<String, LinkedList<String>>());
}

private List<String> DFS(String s, Set<String> wordDict, HashMap<String, LinkedList<String>>map) {
if (map.containsKey(s))
return map.get(s);

LinkedList<String>res = new LinkedList<String>();
if (s.length() == 0) {
res.add("");
return res;
}
for (String word : wordDict) {
if (s.startsWith(word)) {
List<String>sublist = DFS(s.substring(word.length()), wordDict, map);
for (String sub : sublist)
res.add(word + (sub.isEmpty() ? "" : " ") + sub);
}
}
map.put(s, res);
return res;
}
}

Solution 2: not the fastest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
// DFS solution : store unable substring
boolean isSuccess = false;
public List<String> wordBreak(String s, List<String> wordDict) {
List<String> res = new ArrayList<>();
if (s == null || s.length() == 0) return res;

HashSet<String> dic = new HashSet<>();
for (String word: wordDict) dic.add(word);

HashSet<String> noWay = new HashSet<>();
String seq = "";
dfs(s, dic, noWay, seq, res);

return res;
}

private void dfs(String s, HashSet<String> dic, HashSet<String> noWay, String seq, List<String> res){
if (s == null || s.length() == 0){
if (seq != null || seq.length() != 0){
res.add(seq.trim());
isSuccess = true;
}
return;
}
for (int i = 1; i <= s.length(); i++){
String first = s. substring(0, i);
String last = s.substring(i);
if (dic.contains(first) && !noWay.contains(last)){
String tmp = seq;
seq += first + " ";
dfs(last, dic, noWay, seq, res);
if (!isSuccess) noWay.add(last);
seq = tmp;
if (seq.length() == 0 && isSuccess == true) isSuccess = false;
}
}
}
}