Aanagram Puzzle

Check out your console to see it running.

Source

###
Problem: Given a list (array) of words, find all the possible anigrams in that list and return
a two deminitional array of the results.

Assumption: all the strings are valid words.
###

anagrams = (words) ->
  # Store as a map for each words.
  anagrams = {}
  # Create a new array of the sorted strings to use as the key in the array
  sorted = words.map (word) ->
    return word.split('').sort().join('')
  sorted.forEach((st, i) ->
    if (!anagrams.hasOwnProperty(st))
      anagrams[st] = [words[i]]
    else
      anagrams[st].push(words[i])
  )

  results = []
  for key, value of anagrams
    results.push value if value.length > 1
  
  return results


testWords = ['dell', 'ledl', 'abc', 'cba', 'will', 'not', 'match', 'these']
console.log(anagrams(testWords))