How to check if two words are anagrams
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Anagrams are words that have the same letters rearranged in a different order. Understanding how to determine if two words are anagrams is a common problem in computer science, often serving as an excellent exercise in string manipulation and understanding of data structures.
How to Check if Two Words Are Anagrams
Conceptual Overview
To establish whether two words are anagrams, we must ensure that both words contain the exact same characters, with the identical frequencies.
Step-by-step Breakdown
- Normalize the Case: Convert both words to the same case (either all uppercase or all lowercase) to avoid mismatches due to case differences.
- Ignore Non-letter Characters: If necessary, remove any non-letter characters or spaces, as they should not be considered in the context of anagrams.
- Comparison Techniques: There are several ways you can determine if two words are anagrams:
- Sorting Method: Sort the characters of both strings in alphabetical order and compare. If the sorted versions of both strings are identical, they are anagrams.
- Hash Table Method: Count the frequency of each character using a hash table (or dictionary/map) for both words. Compare the frequency distributions.
- Bit Manipulation or Prime Multiplication (Advanced): For educational purposes, these methods involve representing characters using bitwise operations or assigning unique prime numbers, though they are not commonly used due to complexity and limitations with handling large character sets.
Code Examples
Here's how you could implement these methods in Python:
- Sorting Method:
- Hash Table Method:
- Sorting Method: The time complexity is due to the sorting operation, where is the length of the strings.
- Hash Table Method: This has a time complexity of , as it involves iterating over each string to count the frequency of each character.
- Cryptography: Anagram checking is often used in cipher decryption.
- Word Games: Many word games use anagram checks to validate input.
- Natural Language Processing (NLP): Useful in text analysis and manipulations where word permutations are significant.
Related reading
- How to check/find if an item is in a DEQUE
- How to checkout old git commit including all submodules recursively?
- How to code the maximum set packing algorithm?
- How to compare objects by multiple fields
- How to check if XGBoost uses the GPU
- How to check syntax of Python file/script without executing it?
- How to compare two dates?
- How to compute intersection of N sorted sets?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.