package com.heu.wsq.leetcode;
import java.util.*;
public class GroupAnagrams {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String s = new String(chars);
List<String> tmpList = null;
if (map.get(s) == null){
tmpList = new ArrayList<>();
map.put(s, tmpList);
}
tmpList = map.get(s);
tmpList.add(str);
}
List<List<String>> ans = new ArrayList<>();
for (Map.Entry<String, List<String>> stringListEntry : map.entrySet()) {
ans.add(stringListEntry.getValue());
}
return ans;
}
public static void main(String[] args) {
String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
GroupAnagrams ga = new GroupAnagrams();
List<List<String>> ans = ga.groupAnagrams(strs);
}
}