After finishing Project Euler 24, I decided to make a scrabble type of application using /usr/lib/dict as my dictionary what I cat that into a word file. It does take a few seconds, and the dictionary selection isn't that great.
Is there any way I could make it faster, more effective, and with a better dictionary source?
public class Scrabble {
public static ArrayList<String> numbers = new ArrayList<String>();
public static ArrayList<String> numbers2 = new ArrayList<String>() {
};
public static void main(String[] args) {
Scanner dict = null;
try {
dict = new Scanner(new File("words.txt"));
} catch (FileNotFoundException ex) {
}
while (dict.hasNextLine()) {
numbers.add(dict.nextLine());
}
String n = "gojy";//random text here
rearrange("", n);
LinkedHashSet<String> listToSet = new LinkedHashSet<String>(numbers2);
ArrayList<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
for (int i = 0; i < listWithoutDuplicates.size(); i++) {
if (numbers.contains(listWithoutDuplicates.get(i))) {
System.out.println(listWithoutDuplicates.get(i));
}
}
}
public static void rearrange(
String q, String w) {
if (w.length() <= 1) {
String k = q + w;
numbers2.add(k);//full word
numbers2.add(q);//smaller combination to get words with less letters. doesn't work too well
} else {
for (int i = 0; i < w.length(); i++) {
String c = w.substring(0, i)
+ w.substring(i + 1);
rearrange(q
+ w.charAt(i), c);
}
}
}
}