1. 程式人生 > >[leetcode]49. Group Anagrams

[leetcode]49. Group Anagrams

Solution 1 :兩個hashmap+暴力沒成功

100/101 最後一個testcase超時了,mmp
時間複雜度n*n了

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        
        boolean[] used=new boolean[strs.length];
        List<List<String>> res=new ArrayList<>();
        List<String> kong=new ArrayList<>();
       
        
        for(int i=0;i<strs.length;i++){
            if(used[i]==true) continue;
             if(strs[i].equals("")){
                    kong.add("");
                    used[i]=true;
                    continue;
            }
            
            HashMap<Character,Integer> map1=new HashMap<>();
            for(int k=0;k<strs[i].length();k++){
                int value=map1.getOrDefault(strs[i].charAt(k),0);
                map1.put(strs[i].charAt(k),value+1);
            }
            used[i]=true;
            List<String> temp=new ArrayList<>();
            temp.add(strs[i]);
           
            for(int j=i+1;j<strs.length;j++){
                if(used[j]==true)continue;
                
            
                
                String s=strs[j];
                  if(s.equals("")){
                    kong.add("");
                    used[j]=true;
                    continue;
                }
               
                
                
                HashMap<Character,Integer> map2=new HashMap<>();
                for(int k=0;k<s.length();k++){
                    int value=map2.getOrDefault(s.charAt(k),0);
                    map2.put(s.charAt(k),value+1);
                }
                
                int flag=1;
                if(map2.size()!=map1.size())flag=0;
                for(int k=0;k<s.length();k++){
                    if(map2.get(s.charAt(k))!=map1.get(s.charAt(k))){
                        flag=0;
                        break;
                    }
                   
                }
                if(flag==1){
                    used[j]=true;
                    temp.add(s);
                }

               
            }
            
           res.add(temp);
            
        }
        if(kong.size()!=0)res.add(kong);
        return res;
    }
}

Solution 2: key為sort的string

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
    
        List<List<String>> res=new ArrayList<>();
        HashMap<String, List> map=new HashMap<>();
        
        for(int i=0;i<strs.length;i++){
            char[] temp=strs[i].toCharArray();
            Arrays.sort(temp);
            String key=String.valueOf(temp);
            
            if(!map.containsKey(key)){
                map.put(key,new ArrayList());
            }
            
            map.get(key).add(strs[i]);
            
        }
       
        
       
        return new ArrayList(map.values());
    }
}

Solution 3:key為字母的count

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) return new ArrayList();
        Map<String, List> ans = new HashMap<String, List>();
        int[] count = new int[26];
        for (String s : strs) {
            Arrays.fill(count, 0);
            for (char c : s.toCharArray()) count[c - 'a']++;

            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < 26; i++) {
                sb.append('#');
                sb.append(count[i]);
            }
            String key = sb.toString();
            if (!ans.containsKey(key)) ans.put(key, new ArrayList());
            ans.get(key).add(s);
        }
        return new ArrayList(ans.values());
    }
}