Checking for permutations in Python