My Java solution, not the shortest, but really fast


  • 0
    Z
    public class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            HashMap<Integer,List<String>> hm = new HashMap<Integer,List<String>>();
            List<List<String>> result = new ArrayList<List<String>>();
            for(int i = 0;i<strs.length;i++){
                int value=0;
                int multvalue = 1;
                for(char c:strs[i].toCharArray()){
                    value += (int)c;
                    multvalue*=(int)c;
                }
                value+=multvalue+strs[i].length();
                if(hm.containsKey(value)){
                    hm.get(value).add(strs[i]);
                    continue;
                } 
                List<String> temp = new ArrayList<String>();
                temp.add(strs[i]);
                hm.put(value,temp);
            }
            Set set = hm.entrySet();
            Iterator i = set.iterator();
            while(i.hasNext()) {
                Map.Entry me = (Map.Entry)i.next();
                result.add((ArrayList<String>)me.getValue());
            }
            return result;
        }
        
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.