Skip to content

Instantly share code, notes, and snippets.

@Roger7410
Created October 14, 2016 00:04
Show Gist options
  • Select an option

  • Save Roger7410/0629b2b5ef39879239d60d0675608ff3 to your computer and use it in GitHub Desktop.

Select an option

Save Roger7410/0629b2b5ef39879239d60d0675608ff3 to your computer and use it in GitHub Desktop.
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if(strs.length < 1 || strs == null) return new ArrayList<List<String>>();
Map<String,List<String>> map = new HashMap<>();
for(String s: strs){
char[] chars = s.toCharArray();
Arrays.sort(chars);
String key = String.valueOf(chars);
if(!map.containsKey(key)) map.put(key, new ArrayList<String>());
map.get(key).add(s);
}
return new ArrayList<List<String>>(map.values());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment