public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> result = new ArrayList<List<String>>();
if (strs == null || strs.length == 0) {
return result;
}
Map<String, List<String>> map = new HashMap<String, List<String>>();
Arrays.sort(strs);
for (int i = 0; i < strs.length; i++) {
char[] chArray = strs[i].toCharArray();
Arrays.sort(chArray);
String s = String.valueOf(chArray);
if (map.get(s) != null) {
map.get(s).add(strs[i]);
} else {
List<String> list = new ArrayList<String>();
list.add(strs[i]);
map.put(s, list);
}
}
for (List<String> ls : map.values()) {
result.add(ls);
}
return result;
}
}