Problem
Given an array of strings, group anagrams together.
Example:
1 |
|
Note:
- All inputs will be in lowercase.
- The order of your output does not matter.
Explanation
-
We want to put all same anagrams into a list, so we can sort all strings, then if two sorted strings are equal, then they have the same anagram.
-
Iterating each string, we can get the string then make it as a char array then do sorting. We put the sorted string into a
hashmap<String, List<String>>
as key, and the original string as value. -
After finish iteration, we can retun the hashmap’s values as a new array list as the result.
Solution
1 |
|