【哈希】49.字母异位词分组

力扣

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            String hash = getHash(str);
            map.computeIfAbsent(hash, k -> new ArrayList<>()).add(str);
        }

        return new ArrayList<>(map.values());
    }

    public String getHash(String str) {
        char[] array = str.toCharArray();
        Arrays.sort(array);
        return String.valueOf(array);
    }
}

你可能感兴趣的:(力扣Top100,哈希算法,java,算法)