Introduction
The Valid Anagram problem involves checking whether two strings contain the same characters with the same frequencies.
Two strings are called anagrams if:
- both strings contain identical characters
- character frequencies are same
- order may differ
Given two strings:
- s
- t
the task is to determine whether they are anagrams.
This problem helps in understanding:
- hashing
- frequency counting
- sorting techniques
- string comparison
Example
Input:s = "listen"
t = "silent"
Output:
True
Explanation:
Both strings contain:
l,i,s,t,e,n
with same frequencies.
Input:
s = "hello"
t = "world"
Output:
False
Explanation:
Character frequencies are different.
Constraints
1 <= s.length, t.length <= 5 * 10^4s and t contain lowercase English lettersApproach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Compare frequency of every character manually
- Ensure frequencies match in both strings
This approach works but repeated counting increases time complexity.
Steps
- Check string lengths.
- Traverse characters of first string.
- Count frequency in both strings.
- If frequencies differ:
- return false
- Otherwise:
- return true
Dry Run
Input:s = "listen"
t = "silent"
Check character l:
Frequency in s = 1
Frequency in t = 1
Check character i:
Frequency matches
Continue traversal...
All frequencies match
Final Result:
True
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)Explanation:
Frequencies are repeatedly counted.
Space Complexity: O(1)
Explanation:
No extra data structures are used.
Approach 2 : Better Solution (Sorting)
Explanation
If two strings are anagrams:
- their sorted forms become identical
The idea is:
- Sort both strings
- Compare sorted strings
This reduces repeated frequency counting.
Steps
- Check string lengths.
- Sort first string.
- Sort second string.
- Compare sorted strings.
- Return result.
Dry Run
Input:s = "listen"
t = "silent"
Sort strings:
"listen" → "eilnst"
"silent" → "eilnst"
Compare:
Both are equal
Final Result:
True
Better Code
Complexity Analysis
Time Complexity: O(n log n)Explanation: Sorting is performed on both strings.
Space Complexity: O(n)
Explanation: Extra space may be used during sorting.
Approach 3 : Optimized Solution (Hashing)
Explanation
The optimized solution uses frequency counting.
The idea is:
- Count frequencies of characters
- Compare frequencies
If frequencies match:
- strings are anagrams
This provides:
- O(n) time complexity
Steps
- Check string lengths.
- Create frequency hashmap.
- Increment frequency using first string.
- Decrement frequency using second string.
- Check all frequencies become 0.
Dry Run
Input:s = "listen"
t = "silent"
Store frequencies:
l → +1
i → +1
s → +1
t → +1
e → +1
n → +1
Process second string:
s → -1
i → -1
l → -1
e → -1
n → -1
t → -1
All frequencies become 0
Final Result:
True
Optimized Code
Complexity Analysis
Time Complexity: O(n)Explanation: Each character is processed once.
Space Complexity: O(n)
Explanation: Extra hashmap is used for frequencies.
Edge Cases
- Strings have different lengths
- Strings contain repeated characters
- Strings contain one character
- Strings already equal
- Strings contain special characters
Why This Problem is Important
This problem helps in understanding:
- Hashing
- Frequency counting
- Sorting techniques
- String comparison
- Optimized traversal
It is one of the most important string hashing interview problems.
Real-World Applications
Anagram concepts are used in:
- Search engines
- Spell checking systems
- NLP systems
- Pattern matching
- Data grouping systems
Common Mistakes
- Forgetting length check
- Incorrect hashmap updates
- Sorting wrong strings
- Frequency mismatch handling
Interview Tips
Interviewers often expect:
- Hashing optimization
- Frequency counting logic
- O(n) solution explanation
Always explain why equal frequencies guarantee an anagram.
Related Questions
- Group Anagrams
- Palindrome
- Character Frequency
- Ransom Note
- First Unique Character
Final Takeaway
The Valid Anagram problem is a fundamental hashing problem that teaches frequency counting and optimized string comparison techniques. Understanding this problem builds a strong foundation for advanced hashing and string processing interview problems.