This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathLongestCompoundWord.java
More file actions
62 lines (57 loc) · 1.86 KB
/
Copy pathLongestCompoundWord.java
File metadata and controls
62 lines (57 loc) · 1.86 KB
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
import java.util.ArrayList;
import java.util.HashMap;
public class LongestCompoundWord {
HashMap<String, String> suffixTree = new HashMap<String, String>();
ArrayList<String[]> queue = new ArrayList<String[]>();
public static void main(String[] args) {
LongestCompoundWord lcw = new LongestCompoundWord();
String[] input = { "cat", "cats", "catsdogcats", "catxdogcatsrat",
"dog", "dogcatsdog", "hippopotamuses", "rat", "ratcat",
"ratcatdog", "ratcatdogcat" };
System.out.println(lcw.getWord(input));
}
public String getWord(String[] input) {
String LongestWord = "";
// First round
for (int i = 0; i < input.length; i++) {
ArrayList<String[]> suffixes = getSuffixesForPrefixes(input[i],
input[i]);
if (suffixes.size() > 0) {
// Add the pair of original word and suffix to the queue
queue.addAll(suffixes);
}
suffixTree.put(input[i], input[i]);
}
// Empty the queue while searching for longest word
while (!queue.isEmpty()) {
String[] probePair = queue.get(0);
// Find possible valid and compound words
if (suffixTree.containsKey(probePair[1])) { // Valid word
if (probePair[0].length() > LongestWord.length())
LongestWord = probePair[0];
} else {
ArrayList<String[]> suffixes = getSuffixesForPrefixes(
probePair[0], probePair[1]);
if (suffixes.size() > 0) {
// Add the pair of original word and suffix to the queue
queue.addAll(suffixes);
}
}
queue.remove(0);
}
return LongestWord;
}
public ArrayList<String[]> getSuffixesForPrefixes(String originalWord,
String word) {
ArrayList<String[]> suffixes = new ArrayList<String[]>();
for (int i = 1; i <= word.length(); i++) {
if (suffixTree.containsKey(word.substring(0, i))) {
String[] pair = new String[2];
pair[0] = originalWord;
pair[1] = word.substring(i);
suffixes.add(pair);
}
}
return suffixes;
}
}