Home
DSA Visualizer
Strings
Anagram Check
Anagram Check
Visualize checking if two strings are anagrams (contain same characters with same frequencies).
ONE SHARED TALLY
COUNT
0
1
2
3
4
5
0
1
2
3
4
5
Character counts
e
i
l
n
s
t
'l' in the first word → count 1.
Next
'i' in the first word → count 1.
First
Second
Intuition
Two strings are anagrams if they have the exact same characters with the exact same frequencies. We can use a hash map (or frequency array) to count characters in the first string and decrement for the second.
Concept
- Frequency Map: Stores count of each character.
- Process: +1 for char in String 1, -1 for char in String 2.
- Result: If map is empty (or all zeros), it's an anagram.
How it Works
Algorithm:
- If lengths differ, return False.
- Create a frequency map.
- Loop through String 1: Increment count for each char.
- Loop through String 2: Decrement count for each char.
- If any count becomes negative (or char not found), return False.
- Return True.
Step-by-Step Breakdown
Watch the visualization:
- See counts go UP as we scan String 1.
- See counts go DOWN as we scan String 2.
- If all counts return to 0, it's a match.
When to Use
- Checking if two words are rearrangements of each other.
- Grouping anagrams from a list.
When NOT to Use
- When order matters (not anagrams).
How to Identify
"Rearrange characters", "Permutation of string".
Applying for software engineering roles? Free, no sign-up:
