【leetcode毎日ブラシ問題】49.Group Anagrams
2112 ワード
Given an array of strings, group anagrams together.
Example:
Note: All inputs will be in lowercase. The order of your output does not matter.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
class num49{
public static void main(String[] args) {
String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
List> list = num49.groupAnagrams(strs);
for(List strlist:list){
for(Object s:strlist){
System.out.print(s+" ");
}
System.out.println('
');
}
}
//
public static List> groupAnagrams1(String[] strs) {
Map> map = new HashMap>();
for(int i=0; i());
}
map.get(key).add(strs[i]);
}
return new ArrayList(map.values());
}
//
public static List> groupAnagrams2(String[] strs) {
Map> map = new HashMap>();
for(String str: strs){
int[] count = new int[26];
for(int i=0; i list = new ArrayList<>();
list.add(str);
map.put(key, list);
}else{
map.get(key).add(str);
}
}
return new ArrayList(map.values());
}
}