标签:
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Return:
[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]
Note: All inputs will be in lower-case.
public class Solution { public List<List<String>> groupAnagrams(String[] strs) { Map<String, List<String>> map = new HashMap<>(); for(String s : strs) { char[] key = s.toCharArray(); Arrays.sort(key); String sKey = new String(key); List<String> dest = map.get(sKey); if(dest == null) { dest = new ArrayList<String>(); map.put(sKey, dest); } dest.add(s); } List<List<String>> ret = new ArrayList<>(); for(List<String> group: map.values()) { ret.add(group); } return ret; } }
标签:
原文地址:http://www.cnblogs.com/neweracoding/p/5574653.html