-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.java
More file actions
99 lines (89 loc) · 2.88 KB
/
Copy pathAnagrams.java
File metadata and controls
99 lines (89 loc) · 2.88 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
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
import java.util.*;
import java.io.*;
public class Anagrams {
/* HashMap has the following mapping:
KEY: Sorted word (universal key for all anagrams)
VALUE: TreeSet for all dictionary words that are anagrams */
private static HashMap<String, TreeSet<String>> map = new HashMap<>();
/* private method to process provided dictionary file & update map
PARAMS: arraylist<string> of read in words from txt file
RETURN: (void) */
private static void processDictionary(ArrayList<String> originalDict) {
for (int i = 0; i < originalDict.size(); i++) {
String originalWord = originalDict.get(i);
// sorts original word to obtain "universal" anagramKey
String anagramKey = sortString(originalWord);
// adds the original word into hashmap's treeset, under anagramKey
TreeSet<String> wordsList;
if (!map.containsKey(anagramKey)) {
wordsList = new TreeSet<>();
wordsList.add(originalWord);
map.put(anagramKey, wordsList);
} else {
wordsList = map.get(anagramKey);
wordsList.add(originalWord);
}
}
}
/* private helper method to sort Strings
PARAMS: String (original word from dictionary)
RETURN: String (sorted word) */
private static String sortString(String str) {
char[] temp = str.toCharArray();
Arrays.sort(temp);
return new String(temp);
}
/* private method to create final output String of anagrams
PARAMS: String (original word from dictionary)
RETURN: (void) */
private static void displayAnagrams(String w) {
String anagramKey = sortString(w);
TreeSet<String> anagrams = map.get(anagramKey);
if (anagrams != null) {
int count = 0;
int size = anagrams.size() - 1;
StringBuilder output = new StringBuilder();
for (String s : anagrams) {
output.append(s);
output.append(" ");
}
System.out.println(output.toString());
} else {
System.out.println('-');
}
}
private static void testDictionary(ArrayList<String> original) {
// for (Map.Entry e : map.entrySet()) {
// System.out.println(e.getKey());
// System.out.println(e.getValue());
// }
}
public static void main(String[] args) {
// read in provided file path parameter to dictionary
try {
File file = new File(args[0]);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String word;
ArrayList<String> original = new ArrayList<>();
while ((word = bufferedReader.readLine()) != null) {
original.add(word);
}
fileReader.close();
// process dictionary and update HashMap map
processDictionary(original);
// testDictionary(original);
} catch (IOException e) {
e.printStackTrace();
}
// read in input from command line & process outputs
Scanner scan = new Scanner(System.in);
System.out.print("> ");
String input = scan.nextLine();
while (!(input.equals(""))) {
displayAnagrams(input);
System.out.print("> ");
input = scan.nextLine();
}
}
}