Refactor anagrams solution.