Skip to main content

🟡 剑指 Offer II 033. 变位词组

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解1#

实际上是工程题目,把每个单词的字符+出现数转成key值,就可以用哈希表来去重了

class Solution {    public List<List<String>> groupAnagrams(String[] strs) {        Map<String, List<String>> groups = new HashMap<>();
        for (String cur : strs) {            int[] counter = new int[26];            for (char c : cur.toCharArray()) {                counter[c-'a'] += 1;            }            StringBuilder sb = new StringBuilder();            for (int i=0; i<26; i+=1) {                if (counter[i] > 0) {                    sb.append((char)'a'+i);                    sb.append(counter[i]);                }            }            String key = sb.toString();            List<String> val = groups.getOrDefault(key, new ArrayList<>());            val.add(cur);            groups.put(key, val);        }
        return new ArrayList<>(groups.values());    }}