-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcatenatedWords.java
56 lines (47 loc) · 1.86 KB
/
ConcatenatedWords.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
/*
Concatenated Words
Given an array of strings words (without duplicates), return all the concatenated words in the given list of words.
A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.
Example 1:
Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
Example 2:
Input: words = ["cat","dog","catdog"]
Output: ["catdog"]
Constraints:
1 <= words.length <= 104
1 <= words[i].length <= 30
words[i] consists of only lowercase English letters.
All the strings of words are unique.
1 <= sum(words[i].length) <= 105
*/
/* https://leetcode.com/problems/concatenated-words/description/ */
class ConcatenatedWords {
public List<String> findAllConcatenatedWordsInADict(String[] words) {
List<String> ans = new ArrayList<>();
Set<String> wordSet = new HashSet<>(Arrays.asList(words));
Map<String, Boolean> memo = new HashMap<>();
for (final String word : words)
if (wordBreak(word, wordSet, memo))
ans.add(word);
return ans;
}
private boolean wordBreak(final String word, Set<String> wordSet, Map<String, Boolean> memo) {
if (memo.containsKey(word))
return memo.get(word);
for (int i = 1; i < word.length(); ++i) {
final String prefix = word.substring(0, i);
final String suffix = word.substring(i);
if (wordSet.contains(prefix) &&
(wordSet.contains(suffix) || wordBreak(suffix, wordSet, memo))) {
memo.put(word, true);
return true;
}
}
memo.put(word, false);
return false;
}
}