Skip to content

Instantly share code, notes, and snippets.

@eeroan
Created February 13, 2013 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eeroan/4780294 to your computer and use it in GitHub Desktop.
Save eeroan/4780294 to your computer and use it in GitHub Desktop.
Anagram tester algoritm
function anagramTester(dictionary) {
var hash = _.groupBy(dictionary, sortStr)
return function(word) {
var sortedWord = sortStr(word)
return (sortedWord in hash) ? hash[sortedWord] : []
}
function sortStr(str) { return _.map(str, _.identity).sort().join('') }
}
var tester = anagramTester(['dog','god','cat','tag','good'])
tester('dog') //returns ["dog", "god"]
tester('cat') //returns ["cat"]
tester('foo') //returns []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment