Skip to content

Commit eae1dde

Browse files
[N-0] refactor 140
1 parent 01872ff commit eae1dde

File tree

1 file changed

+12
-35
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+12
-35
lines changed

src/main/java/com/fishercoder/solutions/_140.java

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5-
import java.util.HashSet;
65
import java.util.List;
7-
import java.util.Set;
86

97
/**
8+
* 140. Word Break II
9+
*
1010
* 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.
1111
1212
Return all such possible sentences.
@@ -19,54 +19,31 @@
1919
2020
*/
2121
public class _140 {
22-
public List<String> wordBreak(String s, Set<String> wordDict) {
23-
return dfs(s, wordDict, new HashMap<String, ArrayList<String>>());
22+
public List<String> wordBreak(String s, List<String> wordDict) {
23+
return dfs(s, wordDict, new HashMap<>());
2424
}
2525

26-
private List<String> dfs(String s, Set<String> wordDict,
27-
HashMap<String, ArrayList<String>> map) {
26+
List<String> dfs(String s, List<String> wordDict, HashMap<String, List<String>> map) {
2827
if (map.containsKey(s)) {
2928
return map.get(s);
3029
}
3130

32-
ArrayList<String> res = new ArrayList<String>();
31+
ArrayList<String> result = new ArrayList<>();
3332
if (s.length() == 0) {
34-
res.add("");
35-
return res;
33+
result.add("");
34+
return result;
3635
}
3736

3837
for (String word : wordDict) {
3938
if (s.startsWith(word)) {
4039
List<String> subList = dfs(s.substring(word.length()), wordDict, map);
4140
for (String sub : subList) {
42-
res.add(word + (sub.length() == 0 ? "" : " ") + sub);
41+
result.add(word + (sub.length() == 0 ? "" : " ") + sub);
4342
}
4443
}
4544
}
46-
map.put(s, res);
47-
return res;
45+
map.put(s, result);
46+
return result;
4847
}
4948

50-
public static void main(String... strings) {
51-
List<String> temp = new ArrayList<String>();
52-
System.out.println(temp);
53-
List<String> temp2 = new ArrayList<String>(temp);
54-
temp2.add("");
55-
System.out.println(temp2);
56-
57-
_140 test = new _140();
58-
Set<String> wordDict = new HashSet();
59-
wordDict.add("cat");
60-
wordDict.add("cats");
61-
wordDict.add("sand");
62-
wordDict.add("and");
63-
wordDict.add("dog");
64-
String s = "catsanddog";
65-
// List<String> list = test.wordBreak(s, wordDict);
66-
List<String> list = test.wordBreak(s, wordDict);
67-
for (String word : list) {
68-
System.out.print(word + ", ");
69-
}
70-
System.out.println();
71-
}
72-
}
49+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy