a = "hello"
b = "olhel"
print sorted(a) == sorted(b)
Glenn Maynard
2010-09-30 00:26:23
An O(n) algorithm is to create a dictionary of counts of each letter and then compare the dictionaries.
In Python 2.7 or newer this can be done using collections.Counter
:
>>> from collections import Counter
>>> Counter('hello') == Counter('olhel')
True