24ms short java solution


  • 0
    R
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            char[] ar = s.toCharArray();
            Arrays.sort(ar);
            String sorted = String.valueOf(ar);
            List<String> list = map.get(sorted);
            if (list == null) list = new ArrayList<String>();
            list.add(s);
            map.put(sorted, list);
        }
        List<List<String>> res = new ArrayList<>();
        for (List<String> l : map.values()) {
            Collections.sort(l);
            res.add(l);
        }
        return res;
    }

  • 0
    A

    What is the complexity of your code?


Log in to reply
 

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