-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution3.java
90 lines (85 loc) · 2.25 KB
/
Solution3.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
/*
* Word break ||
* s = "catsanddog",
* dict = ["cat", "cats", "and", "sand", "dog"].
* A solution is ["cats and dog", "cat sand dog"].
*/
public class Solution3 {
/*
public ArrayList<String> wordBreak(String s, Set<String> dict) {
// Note: The Solution object is instantiated only once and is reused by
// each test case.
ArrayList<String> result = new ArrayList<String>();
if (s == null || s.length() == 0) {
return result;
}
int length = s.length();
dfs(s, 0, 0, length, dict, result, new StringBuffer());
return result;
}*/
public ArrayList<String> wordBreak(String s, Set<String> dict) {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < s.length(); i++) {
boolean key = false;
for (String d : dict) {
if (d.indexOf(s.charAt(i)) >= 0) {
key = true;
break;
}
}
if (!key)
return result;
}
for (String str : dict) {
if (s.startsWith(str)) {
dfs(s.replaceFirst(str, ""), str, dict, result);
}
}
return result;
}
public void dfs(String s, String left, Set<String> dict, ArrayList<String> result) {
if (s.equals(""))
result.add(left);
else {
for (String str : dict) {
if (s.startsWith(str))
dfs(s.replaceFirst(str, ""),left+" "+str,dict,result);
}
}
}
/*
public void dfs(String s, int start, int depth, int length,
Set<String> dict, ArrayList<String> result, StringBuffer sb) {
if (depth == length) {
String t = sb.toString();
result.add(t.substring(0, t.length() - 1));
return;
}
for (int len = 1; len <= length - start; len++) {
String t = s.substring(start, start + len);
if (dict.contains(t)) {
int beforeAddLen = sb.length();
sb.append(t).append(" ");
dfs(s, start + len, depth + len, length, dict, result, sb);
sb.delete(beforeAddLen, sb.length());
}
}
}*/
public static void main(String... args) {
Solution3 s3 = new Solution3();
String s = "catsanddog";
Set<String> set = new HashSet<String>();
set.add("cat");
set.add("cats");
set.add("and");
set.add("sand");
set.add("dog");
ArrayList<String> list = new ArrayList<String>();
list = s3.wordBreak(s, set);
for (String l : list)
System.out.println(l);
}
}