In a previous post, we talked about ways of checking whether 2 strings are anagrams using Counter() from the collections module.
In this article, we are going to mention another way of checking whether 2 strings are anagrams using sorted() method.
In case you need a reminder what an anagram is, here is a quick recap:
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode.
In fact, it is pretty straightforward.
All you have to do is import sort both lists, and assuming that you don’t care about the case sensitivity of characters in the strings, here is a quick implementation:
Yes, that’s quite simple and intuitive.
Now, here are some tests that you can use:
I hope you find this useful.