Share my java solution, use sort and map

  • 0
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
        for(String str : strs){
            char[] chs = str.toCharArray();
            String s = String.valueOf(chs);
            if(! map.containsKey(s)){
                map.put(s, new ArrayList<String>());
        for(List<String> list : map.values()){
        return res;

Log in to reply

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