02 - Valid Anagrams
Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
An anagram is a string that contains exactly the same characters with the same frequency, but the order of characters can be different.
Examples¶
Example 1
Input
Output
Explanation
Both strings contain the same characters: r, a, c, e with the same frequencies.
Example 2
Input
Output
Explanation
The characters differ (r vs m), so they are not anagrams.
Solution¶
Java Code¶
import java.util.HashMap;
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
HashMap<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (char c : t.toCharArray()) {
if (!map.containsKey(c)) {
return false;
}
map.put(c, map.get(c) - 1);
if (map.get(c) == 0) {
map.remove(c);
}
}
return map.isEmpty();
}
}
Intuition¶
Two strings are anagrams if:
-
They have the same length
-
They have the same characters
-
Each character appears the same number of times
So the idea is:
-
Count the frequency of each character in string
s. -
Reduce the frequency using characters from string
t. -
If every character cancels out perfectly, the map will be empty.
Steps:
-
If lengths differ → immediately return
false. -
Build a frequency map from
s. -
Traverse
tand decrement counts. -
If a character is missing or frequency mismatch occurs → return
false. -
If map becomes empty → strings are anagrams.
Java Collections Framework Component Used¶
1. HashMap¶
What it is¶
HashMap is a key-value based data structure in the Java Collections Framework.
It stores pairs of keys and values.
In this problem:
Example
Frequency map becomes:
Why HashMap is used¶
HashMap is used because:
-
It allows efficient frequency counting
-
It provides O(1) average lookup time
-
It can store character → frequency mapping
Time complexity for operations:
| Operation | Time Complexity |
|---|---|
| put() | O(1) |
| get() | O(1) |
| containsKey() | O(1) |
| remove() | O(1) |
How it is used in the solution¶
1. Creating the Map¶
This stores character frequencies.
2. Counting Characters¶
getOrDefault() is a useful JCF method.
Meaning:
-
If key exists → return its value
-
If key does not exist → return default value
Example
3. Checking Characters in Second String¶
If a character from t doesn't exist in s, they cannot be anagrams.
4. Decreasing Frequency¶
Each matching character cancels one occurrence.
5. Removing Zero Frequency¶
When frequency becomes zero, the key is removed.
6. Final Check¶
If all characters cancel out, the map becomes empty.
Complexity Analysis¶
Time Complexity¶
Where n is the length of the string.
We traverse both strings once.
Space Complexity¶
Where k is the number of unique characters.
For lowercase English letters, k ≤ 26.
Key Takeaways¶
-
Anagrams require equal character frequency.
-
HashMapis ideal for frequency counting problems. -
getOrDefault()is commonly used in JCF for counting patterns. -
This solution runs in linear time O(n) and is optimal.