for( Map.Entry<Integer,Set<String>> e : theMap.entrySet( ) )
{
int theWordLen = e.getKey( );
Set<String> theWords = e.getValues( );
// You will need more work for the next line to handle theWordLen == 1
Set<String> shorterWords = theMap.get( theWordLen - 1 );
for( String s : theWords )
{
for( int i = 0; i < theWordLen; i++ )
{
String shorter = removeOneChar( s, i );
if( shorterWords.contains( shorter ) )
{
ADD TWO EDGES TO THE GRAPH
}
}
}
}
for( Map.Entry<Integer,Set<String>> e : theMap.entrySet( ) )
{
int theWordLen = e.getKey( );
Set<String> theWords = e.getValues( );
String [ ] words = new String[ theWords.size( ) ];
theWords.toArray( words );
for( int i = 0; i < theWordLen; i++ )
{
SORT words by using a comparator that ignores position i
FIND cliques of adjacent words (in the array) that are
identical except for position i and ADD EDGES
}
}
To see how this algorithm works, consider words [ "eat", "fat", "fit", "fan", "far", "for" ].
When these words are sorted, and character 0 is ignored, we get
[ "fan", "far", "eat", "fat", "fit", "for" ]. In this array, "eat" and "fat" are adjacent to
each other and are identical except for position 0, so edges between them
are added to the graph at cost 1.
Then we sort, ignoring character 1, and we get
[ "eat", "fan", "far", "for", "fat", "fit" ]. In this array "far" and "for" are adjacent
to each other and identical except for position 1, so edges between them
are add to the graph. The same goes for "fat" and "fit".
Finally, we sort ignoring position 2. In this case, we get
[ "eat", "fan", "far", "fat", "fit", "for" ]. Here, the clique "fan", "far", "fat"
all are adjacent and all are identical except for position 2. So we add
edges between every pair of words (a total of SIX directed edges).