How can I check if a string has the same characters? Python

python, python-2.7, string

Solution

Sort the two strings and then compare them:

sorted(str1) == sorted(str2)

If the strings might not be the same length, you might want to make sure of that first to save time:

len(str1) == len(str2) and sorted(str1) == sorted(str2)

Problem

I need to be able to discern if a string of an arbitrary length, greater than 1 (and only lowercase), has the same set of characters within a base or template string. For example, take the string "aabc": "azbc" and "aaabc" would be false while "acba" would be true. Is there a fast way to do this in python without keeping track of all the permutations of the first string and then comparing it to the test string?

Original source