My java SOLUTION


  • 0
    B
    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            HashMap<String, Integer> map = new HashMap<>();
            List<List<String>> ans = new ArrayList<>();
            for(int i = 0; i < strs.length; i++){
                char[] tmp = strs[i].toCharArray();
                Arrays.sort(tmp);
                String str = new String(tmp);
                if(map.containsKey(str)){
                    int idx = map.get(str);
                    ans.get(idx).add(strs[i]);
                }
                else{
                    map.put(str, ans.size());
                    List<String> list = new ArrayList<>();
                    list.add(strs[i]);
                    ans.add(list);
                }
            }
            return ans;
        }
    }
    

Log in to reply
 

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