Java straight forward solution using map


  • 0
    Y

    Instead of sorting the original string array, we can sort the map valueSet, ideas get from @sreenidhi2.

    public class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            List<List<String>> res = new ArrayList<>();
            Map<String, List<String>> map = new HashMap<>();
            for(String s : strs) {
                char[] arr = s.toCharArray();
                Arrays.sort(arr);
                String t = String.valueOf(arr);
                if(map.containsKey(t)) {
                    map.get(t).add(s);
                } else {
                    map.put(t, new ArrayList<String>(Arrays.asList(s)));
                }
            }
            for(String s : map.keySet()) {
                Collections.sort(map.get(s));
                res.add(map.get(s));
            }
            return res;
        }
    }

Log in to reply
 

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