Created
February 13, 2013 00:54
-
-
Save eeroan/4780294 to your computer and use it in GitHub Desktop.
Anagram tester algoritm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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