49. Group Anagrams

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> group = new HashMap<>();
        for (String s : strs) {
            char[] cAry = s.toCharArray();
            Arrays.sort(cAry);
            String sortedKey = String.valueOf(cAry);
            group.computeIfAbsent(sortedKey, k -> new ArrayList<>()).add(s);
        }
        List<List<String>> result = new ArrayList<>();
        result.addAll(group.values());
        return result;
    }
}

/**
O(n*wlogw)
O(n*w) 
 */

https://leetcode.com/problems/group-anagrams/

  1. O(n*wlogw),

2. O(n*w)

easier one

Last updated

Was this helpful?