-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetLetters.java
More file actions
123 lines (87 loc) · 3.18 KB
/
SetLetters.java
File metadata and controls
123 lines (87 loc) · 3.18 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.*;
/**
* SetLetters class creates different combinations of letters and checks whether the word exists in the dictionary
* Stores the word found in the dictionary in the wordFound ArrayList
* @author Adisa Narula
* @version 1.0
*
*/
public class SetLetters {
// Instance of dictionary class to access the content of dictionary file
private Dictionary d;
// initialize array to store words found
ArrayList<String> wordFound = new ArrayList<String>();
/**
* Creates a new instance of dictionary object and creates different words that are searched for in the dictionary
* This constructor calls the createLettersLoop method
* @param letters string array of letters
* @param dictionaryList string array of words in the dictionary
*/
public SetLetters(String[] letters, String[] dictionaryList) {
d = new Dictionary(dictionaryList);
createLettersLoop(letters);
}
/**
* Makes different combinations of word from array of letters and checks whether the word exists in the dictionary
* This method calls the method in the helperDictionary and checkEquals method in the dictionary class
* @param word that is checked for in the dictionary
* @param letters array of letters
*/
public void createLetters(String word, String[] letters) {
// initialize string for new word
String[] tempArray = new String[letters.length];
// loop through letters array
for (int i = 0; i < letters.length; i++) {
// make a copy of letters array
for (int j = 0; j < letters.length; j++) {
tempArray[j] = letters[j];
}
// if the letter in array has already been used, use null as flag
if (tempArray[i] == null) {
continue;
}
// otherwise concatenate to word and flag the letter that was used
String newWord = word + tempArray[i];
tempArray[i] = null;
// check whether prefix of the word is found in dictionary if true call equals function
if (d.prefixDictionary(newWord)) {
if (d.checkEquals(newWord)) {
wordFound.add(newWord);
}
createLetters(newWord, tempArray);
}
}
}
// calls createLetter method with different letters as parameter
/**
* Iterates through every letter in the array and creates different combinations of words starting with that letter
* This method calls createLetters method
* @param lettersInput string array of letters
*/
private void createLettersLoop(String[] lettersInput) {
// make copy of the array
for (int j = 0; j < lettersInput.length; j++) {
String[] temp = new String[lettersInput.length];
for (int i = 0; i < temp.length; i++) {
temp[i] = lettersInput[i];
}
temp[j] = null;
createLetters(lettersInput[j], temp);
}
}
/**
* Removes duplicates and sorts the arrays of words found in the dictionary
* @return words found that are unique and sorted
*/
public Collection <String> getArray() {
// sort the array
Collections.sort(wordFound);
// remove duplicates
for (int i = wordFound.size()-1; i > 0; i--) {
if (wordFound.get(i).equals(wordFound.get(i-1))) {
wordFound.remove(i);
}
}
return wordFound;
}
}