Beat 92% java code!!!


  • 1
    M
    public class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            if(strs.length == 0){
                return new ArrayList<List<String>>();
            }
            List<List<String>> result = new ArrayList<List<String>>();
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            for(int i = 0; i < strs.length; i++){
                String tem = strs[i];
                char[] c = tem.toCharArray();
                Arrays.sort(c);
                tem = String.valueOf(c);
                if(map.containsKey(tem)){
                    int index = map.get(tem);
                    result.get(index).add(strs[i]);
                }else{
                    List<String> res = new ArrayList<String>();
                    res.add(strs[i]);
                    result.add(res);
                    map.put(tem, result.size()-1);
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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