Anagram algorithm with minimum complexity