Skip to content

Instantly share code, notes, and snippets.

@BiruLyu
Created May 22, 2017 22:06
Show Gist options
  • Save BiruLyu/9f50c07cec4d8ffd11b061e0d6588ebc to your computer and use it in GitHub Desktop.
Save BiruLyu/9f50c07cec4d8ffd11b061e0d6588ebc to your computer and use it in GitHub Desktop.
public class Solution {
public String countChar(String str){
int[] vector = new int[26];
for(char c : str.toCharArray()){
vector[c - 'a']++;
}
return Arrays.toString(vector);
}
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String,List<String>> map = new HashMap<String, List<String>>();
for(String str : strs){
String temp = countChar(str);
if(!map.containsKey(temp)){
map.put(temp, new ArrayList<String>());
}
map.get(temp).add(str);
}
return new ArrayList<List<String>>(map.values());
}
}
@ankurbag
Copy link

Wow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment