问题描述(原题链接)
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。。
代码:
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
int index=0;
HashMap<String,Integer> map = new HashMap<String,Integer>();
List<List<String>> list = new ArrayList<List<String>>();
int len = strs.length;
for(int i=0;i<len;i++){
char[] temp = strs[i].toCharArray();
Arrays.sort(temp);
if(map.containsKey(new String(temp))){
int j = map.get(new String(temp));
list.get(j).add(strs[i]);
}else{
map.put(new String(temp),list.size());
List<String> temp2 = new ArrayList<String>();
temp2.add(strs[i]);
list.add(temp2);
}
}
return list;
}
}