final anagram function