|
4 | 4 | import java.util.HashMap;
|
5 | 5 | import java.util.List;
|
6 | 6 |
|
7 |
| -/** |
8 |
| - * 140. Word Break II |
9 |
| -
|
10 |
| - Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, |
11 |
| - add spaces in s to construct a sentence where each word is a valid dictionary word. |
12 |
| - Return all such possible sentences. |
13 |
| -
|
14 |
| - Note: |
15 |
| -
|
16 |
| - The same word in the dictionary may be reused multiple times in the segmentation. |
17 |
| - You may assume the dictionary does not contain duplicate words. |
18 |
| -
|
19 |
| - Example 1: |
20 |
| -
|
21 |
| - Input: |
22 |
| - s = "catsanddog" |
23 |
| - wordDict = ["cat", "cats", "and", "sand", "dog"] |
24 |
| - Output: |
25 |
| - [ |
26 |
| - "cats and dog", |
27 |
| - "cat sand dog" |
28 |
| - ] |
29 |
| -
|
30 |
| - Example 2: |
31 |
| -
|
32 |
| - Input: |
33 |
| - s = "pineapplepenapple" |
34 |
| - wordDict = ["apple", "pen", "applepen", "pine", "pineapple"] |
35 |
| - Output: |
36 |
| - [ |
37 |
| - "pine apple pen apple", |
38 |
| - "pineapple pen apple", |
39 |
| - "pine applepen apple" |
40 |
| - ] |
41 |
| - Explanation: Note that you are allowed to reuse a dictionary word. |
42 |
| -
|
43 |
| - Example 3: |
44 |
| -
|
45 |
| - Input: |
46 |
| - s = "catsandog" |
47 |
| - wordDict = ["cats", "dog", "sand", "and", "cat"] |
48 |
| - Output: |
49 |
| - [] |
50 |
| - */ |
51 |
| - |
52 | 7 | public class _140 {
|
53 |
| - public static class Solution1 { |
54 |
| - public List<String> wordBreak(String s, List<String> wordDict) { |
55 |
| - return dfs(s, wordDict, new HashMap<>()); |
56 |
| - } |
57 |
| - |
58 |
| - List<String> dfs(String s, List<String> wordDict, HashMap<String, List<String>> map) { |
59 |
| - if (map.containsKey(s)) { |
60 |
| - return map.get(s); |
61 |
| - } |
62 |
| - |
63 |
| - ArrayList<String> result = new ArrayList<>(); |
64 |
| - if (s.length() == 0) { |
65 |
| - result.add(""); |
66 |
| - return result; |
67 |
| - } |
| 8 | + public static class Solution1 { |
| 9 | + public List<String> wordBreak(String s, List<String> wordDict) { |
| 10 | + return dfs(s, wordDict, new HashMap<>()); |
| 11 | + } |
68 | 12 |
|
69 |
| - for (String word : wordDict) { |
70 |
| - if (s.startsWith(word)) { |
71 |
| - List<String> subList = dfs(s.substring(word.length()), wordDict, map); |
72 |
| - for (String sub : subList) { |
73 |
| - result.add(word + (sub.length() == 0 ? "" : " ") + sub); |
74 |
| - } |
| 13 | + List<String> dfs(String s, List<String> wordDict, HashMap<String, List<String>> map) { |
| 14 | + if (map.containsKey(s)) { |
| 15 | + return map.get(s); |
| 16 | + } |
| 17 | + |
| 18 | + ArrayList<String> result = new ArrayList<>(); |
| 19 | + if (s.length() == 0) { |
| 20 | + result.add(""); |
| 21 | + return result; |
| 22 | + } |
| 23 | + |
| 24 | + for (String word : wordDict) { |
| 25 | + if (s.startsWith(word)) { |
| 26 | + List<String> subList = dfs(s.substring(word.length()), wordDict, map); |
| 27 | + for (String sub : subList) { |
| 28 | + result.add(word + (sub.length() == 0 ? "" : " ") + sub); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + map.put(s, result); |
| 33 | + return result; |
75 | 34 | }
|
76 |
| - } |
77 |
| - map.put(s, result); |
78 |
| - return result; |
79 | 35 | }
|
80 |
| - } |
81 | 36 | }
|
0 commit comments