Checking whether a word is an anagram of another one can be a question that is quite common in coding interviews.
If you don’t know what an anagram is, here is a definition:
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.
Now, checking this for 2 strings can be done in different ways, but in this article we are going to talk about the way we can implement it using Counter from the collections module:
In fact, it is pretty straightforward. All you have to do is import Counter, 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.