Checking if two strings are permutations of each other in Python

前端 未结 22 2445
旧时难觅i
旧时难觅i 2020-12-08 16:04

I\'m checking if two strings a and b are permutations of each other, and I\'m wondering what the ideal way to do this is in Python. From the Zen of

22条回答
  •  既然无缘
    2020-12-08 16:22

    In Python 3.1/2.7 you can just use collections.Counter(a) == collections.Counter(b).

    But sorted(a) == sorted(b) is still the most obvious IMHO. You are talking about permutations - changing order - so sorting is the obvious operation to erase that difference.

提交回复
热议问题