04 - Group Anagrams
Given an array of strings strs, group all anagrams together into sublists. You may return the output in any order.
An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
Examples¶
Example 1
Input
Output
Example 2
Input
Output
Example 3
Input
Output
[[""]]
````
## Solution
```java
import java.util.*;
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] chars = s.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
map.putIfAbsent(key, new ArrayList<>());
map.get(key).add(s);
}
return new ArrayList<>(map.values());
}
}
````
## Intuition
Two strings are anagrams if their **sorted characters are identical**.
Example:
Since both produce the same sorted representation, they belong to the same group.
Steps:
1. Traverse each word in the array.
2. Convert the word to a character array.
3. Sort the characters.
4. Use the sorted string as a key.
5. Store the original word in a list mapped to that key.
Example mapping:
Finally return all grouped values from the map.
## Java Collections Framework Components Used
### HashMap
Used to group anagrams efficiently.
Structure:
Why `HashMap`:
- Fast lookup and insertion
- Allows grouping based on a computed key
- Average time complexity for operations is **O(1)**
Important methods used:
`putIfAbsent(key, value)`
Creates a new list if the key does not exist.
`get(key)`
Retrieves the list mapped to a key.
---
### ArrayList
Used to store multiple strings belonging to the same anagram group.
Example:
Why `ArrayList`:
- Dynamic size
- Efficient append operations
---
### Arrays.sort()
Used to sort characters in each string.
Example:
The sorted string becomes the grouping key.
## Complexity Analysis
Let:
- `n` = number of strings
- `k` = maximum length of each string
Time Complexity
Space required for storing grouped strings in the hashmap.
Key Takeaways¶
-
Anagrams share the same sorted character representation.
-
Use a
HashMapto group strings efficiently. -
Key idea: sorted string → list of anagrams.
-
Avoids expensive pairwise string comparisons.