Whether two texts are made of the same letters (an anagram) is a question about counts, not order. You keep how many of each letter there are in a dictionary; if the two texts' dictionaries are equal, one is a rearrangement of the other.
def letters(text):count = {}for ch in text:count[ch] = count.get(ch, 0) + 1return countsame = letters(first) == letters(second)
==: they are equal when the same keys hold the same values, whatever order they were added in.Another way is to sort both texts and compare: sorted(first) == sorted(second). It is short and pays the price of sorting (n log n); counting is one pass (n). That is the difference a duel measures.