43 lines
1.2 KiB
Text
43 lines
1.2 KiB
Text
class Main
|
|
{
|
|
// take given word and return a string rearranging characters in order
|
|
static Str toOrderedChars (Str word)
|
|
{
|
|
Str[] chars := [,]
|
|
word.each |Int c| { chars.add (c.toChar) }
|
|
return chars.sort.join("")
|
|
}
|
|
|
|
// add given word to anagrams map
|
|
static Void addWord (Str:Str[] anagrams, Str word)
|
|
{
|
|
Str orderedWord := toOrderedChars (word)
|
|
if (anagrams.containsKey (orderedWord))
|
|
anagrams[orderedWord].add (word)
|
|
else
|
|
anagrams[orderedWord] = [word]
|
|
}
|
|
|
|
public static Void main ()
|
|
{
|
|
Str:Str[] anagrams := [:] // map Str -> Str[]
|
|
// loop through input file, adding each word to map of anagrams
|
|
File (`unixdict.txt`).eachLine |Str word|
|
|
{
|
|
addWord (anagrams, word)
|
|
}
|
|
// loop through anagrams, keeping the keys with values of largest size
|
|
Str[] largestKeys := [,]
|
|
anagrams.keys.each |Str k|
|
|
{
|
|
if ((largestKeys.size < 1) || (anagrams[k].size == anagrams[largestKeys[0]].size))
|
|
largestKeys.add (k)
|
|
else if (anagrams[k].size > anagrams[largestKeys[0]].size)
|
|
largestKeys = [k]
|
|
}
|
|
largestKeys.each |Str k|
|
|
{
|
|
echo ("Key: $k -> " + anagrams[k].join(", "))
|
|
}
|
|
}
|
|
}
|