-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path472.java
105 lines (95 loc) · 3.41 KB
/
472.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
__________________________________________________________________________________________________
sample 26 ms submission
class Solution {
public List<String> findAllConcatenatedWordsInADict(String[] words) {
List<String> res = new LinkedList<>();
Set<String> set = new HashSet<>();
int minlen = Integer.MAX_VALUE;
//build dict
for(String word : words){
if(word.length()>0){
set.add(word);
minlen = Math.min(minlen, word.length());
}
}
//check each word and add good ones to dict
for(String word : words){
if(canForm(word, 0, minlen, set)){
res.add(word);
}
}
return res;
}
//no need to remove and add current word in this setup
private static boolean canForm(String word, int start, int offset, Set<String> set){
for(int mid=start+offset; mid<=word.length()-offset;mid++){
if(set.contains(word.substring(start,mid)) &&
(set.contains(word.substring(mid)) || canForm(word, mid, offset, set))) {//backtrack
return true;
}
}
return false;
}
}
__________________________________________________________________________________________________
sample 27 ms submission
class Solution {
public List<String> findAllConcatenatedWordsInADict(String[] words) {
List<String> rs = new ArrayList<>();
Set<String> set = new HashSet<>();
int minLen = Integer.MAX_VALUE;
for (String w : words) {
if (w.length() > 0) {
set.add(w);
}
minLen = Math.min(minLen, w.length());
}
for (String w : words) {
if (canBreakDfs(0, w.length(), minLen, w, set)) {
rs.add(w);
}
}
return rs;
}
private boolean canBreakDfs(int left, int right, int minLen, String s, Set<String> words) {
for (int i=left+minLen; i<=right-minLen; i++) {
if (words.contains(s.substring(left, i)) &&
(words.contains(s.substring(i, right)) || canBreakDfs(i, right, minLen, s, words))) {
return true;
}
}
return false;
}
}
__________________________________________________________________________________________________
sample 44520 kb submission
class Solution {
public List<String> findAllConcatenatedWordsInADict(String[] words) {
Arrays.sort(words, (String a, String b) -> a.length()-b.length());
Set<String> set = new HashSet<String>();
List<String> ans = new ArrayList<String>();
for(String n : words){
if(check(set,n)){
ans.add(n);
}
set.add(n);
}
Collections.sort(ans);
return ans;
}
private boolean check(Set<String> words, String word){
if(word.equals(""))return false;
boolean[] dp = new boolean[word.length()+1];
dp[0]=true;
for(int end = 1; end <= word.length(); end++){
for(int beg = 0; beg < end; beg++){
if(!dp[beg]) continue;
if(words.contains(word.substring(beg,end))){
dp[end]=true;
break;
}
}
}
return dp[word.length()];
}
}