Ten nine-letter words have been separated into units of three letters. The units have been randomly put in the below list. Can you determine the original 10 words?
ent sen oom ush ile cro
cla rbr ise lis ssr lig
hai mar htn age ess new
sag clo gar ion ing oth
enc ine col our erw cod
Not guaranteed to halt, but does the job in this case (Java):
----------------
package perplexus;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TenWords {
public static final String DICTIONARY = "C:\\Documents and Settings\\David\\My Documents\\Scrabble\\enable.txt";
public static String[] BLOCKS = {
"ent", "sen", "oom", "ush", "ile", "cro",
"cla", "rbr", "ise", "lis", "ssr", "lig",
"hai", "mar", "htn", "age", "ess", "new",
"sag", "clo", "gar", "ion", "ing", "oth",
"enc", "ine", "col", "our", "erw", "cod"};
public static void main(String[] args) throws IOException {
BufferedReader in;
List answer,blocks;
Set words;
String word;
blocks = new ArrayList(Arrays.asList(BLOCKS));
words = new HashSet();
in = new BufferedReader(new FileReader(DICTIONARY));
while ((word=in.readLine())!=null) {
if (valid(word,blocks)) {
words.add(word);
}
}
answer = solver(new ArrayList(),blocks,words);
System.out.println(answer);
}
private static boolean valid(String word, List blocks) {
if (word.length()!=9) return false;
if (!blocks.contains(word.substring(0,3))) return false;
if (!blocks.contains(word.substring(3,6))) return false;
if (!blocks.contains(word.substring(6,9))) return false;
return true;
}
private static List solver(List partialAnswer, List remainingBlocks, Set words) {
Iterator it;
List wordsWithBlock;
Map blocksToWords;
Map.Entry entry;
String block,word;
blocksToWords = setUpMap(remainingBlocks,words);
it = blocksToWords.entrySet().iterator();
while (it.hasNext()) {
entry = (Map.Entry) it.next();
block = (String) entry.getKey();
wordsWithBlock = (List) entry.getValue();
if (wordsWithBlock.size()==1) {
word = (String) wordsWithBlock.get(0);
partialAnswer.add(word);
words.remove(word);
for (int i=0; i<9; i+=3) {
remainingBlocks.remove(word.substring(i,i+3));
words.removeAll((List)blocksToWords.get(word.substring(i,i+3)));
}
break;
}
}
if (partialAnswer.size()<10) return solver(partialAnswer,remainingBlocks,words);
return partialAnswer;
}
private static Map setUpMap(List remainingBlocks, Set words) {
Iterator it;
Map blocksToWords;
String word;
blocksToWords = new HashMap();
it = remainingBlocks.iterator();
while (it.hasNext()) {
blocksToWords.put(it.next(),new ArrayList());
}
it = words.iterator();
while (it.hasNext()) {
word = (String) it.next();
((List)blocksToWords.get(word.substring(0,3))).add(word);
((List)blocksToWords.get(word.substring(3,6))).add(word);
((List)blocksToWords.get(word.substring(6,9))).add(word);
}
return blocksToWords;
}
}
------------------
The program outputs:
[crocodile, classroom, hairbrush, collision, closeness, lightning, otherwise, newsagent, encourage, margarine]