LeetCode Top 100 Liked Questions 49. Group Anagrams (Java版; Medium)
题目描述
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
All inputs will be in lowercase.
The order of your output does not matter.
classSolution{public List<List<String>>groupAnagrams(String[] strs){
List<List<String>> list =newArrayList<>();int n = strs.length;if(n==0){return list;}
HashMap<String, List<String>> map =newHashMap<>();for(String str : strs){
StringBuilder sb =newStringBuilder();int[] count =newint[26];for(char ch : str.toCharArray()){int i = ch -'a';
count[i]++;}for(int a : count){
sb.append(a).append("#");}
String s = sb.toString();if(!map.containsKey(s)){
map.put(s,newArrayList<String>());}
map.get(s).add(str);}
list.addAll(map.values());return list;}}
LeetCode Top 100 Liked Questions 49. Group Anagrams (Java版; Medium)
welcome to my blogLeetCode Top 100 Liked Questions 49. Group Anagrams (Java版; Medium)题目描述Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "b...