Write a function to check whether two given strings are Permutation of each other or not. A Permutation of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are Permutation of each other.
We strongly recommend that you click here and practice it, before moving on to the solution.
Method 1 (Use Sorting)
1) Sort both strings
2) Compare the sorted strings
# Python 3 program to check whether two
# strings are Permutations of each other
# function to check whether two strings
# are Permutation of each other */
def arePermutation(str1, str2):
# Get lenghts of both strings
n1 = len(str1)
n2 = len(str2)
# If length of both strings is not same,
# then they cannot be Permutation
if (n1 != n2):
# Sort both strings
a = sorted(str1)
str1 = ” “.join(a)
b = sorted(str2)
str2 = ” “.join(b)
# Compare sorted strings
for i in range(0, n1, 1):
if (str1[i] != str2[i]):
# Driver Code
if __name__ == ‘__main__’:
str1 = “test”
str2 = “ttew”
if (arePermutation(str1, str2)):
# This code is contributed by
Time Complexity: Time complexity of this method depends upon the sorting technique used. In the above implementation, quickSort is used which may be O(n^2) in worst case. If we use a O(nLogn) sorting algorithm like merge sort, then the complexity becomes O(nLogn)
Method 2 (Count characters)
This method assumes that the set of possible characters in both strings is small. In the following implementation, it is assumed that the characters are stored using 8 bit and there can be 256 possible characters.
1) Create count arrays of size 256 for both strings. Initialize all values in count arrays as 0.
2) Iterate through every character of both strings and increment the count of character in the corresponding count arrays.
3) Compare count arrays. If both count arrays are same, then return true.
The above implementation can be further to use only one count array instead of two. We can increment the value in count array for characters in str1 and decrement for characters in str2. Finally, if all count values are 0, then the two strings are Permutation of each other. Thanks to Ace for suggesting this optimization.
If the possible set of characters contains only English alphabets, then we can reduce the size of arrays to 52 and use str[i] – ‘A’ as an index for count arrays. This will further optimize this method.
Time Complexity: O(n)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if any permutation of a large number is divisible by 8
- Check if two strings are k-anagrams or not
- Check if strings are rotations of each other or not | Set 2
- Program to check if two strings are same or not
- Check whether Strings are k distance apart or not
- Check if two given strings are isomorphic to each other
- Check whether two strings are anagram of each other
- Check if edit distance between two strings is one
- A Program to check if strings are rotations of each other or not
- Check if two strings have a common substring
- Check if there is any common character in two given strings
- Check if all the palindromic sub-strings are of odd length
- Check whether a given string is an interleaving of two other given strings
- Python sorted() to check if two strings are anagram or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.