If I give you scrabble tiles and a dictionary, determine the valid words
from your collection of tiles.
package joshho; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; public class Runner { ScrabbleSet set ; private class ScrabbleSet { char[] chars; public ScrabbleSet(String[] orig){ chars = new char[orig.length]; for(int i=0;i<orig.length;i++) chars[i] = orig[i].charAt(0); Arrays.sort(chars); } public boolean checkWord(char[] find){ if(find.length > chars.length) return false; Arrays.sort(find); int s=0; for(int i=0;i<find.length;i++){ for(int j=s;j<chars.length;j++){ if(find[i] > chars[j]) continue; if(find[i] == chars[j]){ s=j+1; break; } return false; } } return true; } } public void load(String y){ set = new ScrabbleSet(y.split(" ")); } public static void main(String[] args) throws IOException { args = new String[]{"Scrabble\\data\\english4000.dic", "g w x b d u t z q m o i e p o"}; FileReader fr = new FileReader(args[0]); BufferedReader br = new BufferedReader(fr); Runner r = new Runner(); r.load(args[1]); System.out.println(args[1]); String line; while((line = br.readLine()) != null) { System.out.print(line+" "); System.out.println(r.set.checkWord(line.toCharArray())); } br.close(); } } |
There’s probably a better way to do this, I am interested
to see what other solutions there are.