Refactor anagram solution.