字母异位词-leetcode

题目

给你一个字符串数组,请你将字母异位词组合在一起。可以按任意顺序返回结果列表。

示例 1:

输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]

输出: [["bat"],["nat","tan"],["ate","eat","tea"]]

解释:

  • 在 strs 中没有字符串可以通过重新排列来形成 "bat"
  • 字符串 "nat""tan" 是字母异位词,因为它们可以重新排列以形成彼此。
  • 字符串 "ate""eat""tea" 是字母异位词,因为它们可以重新排列以形成彼此。

示例 2:

输入: strs = [""]

输出: [[""]]

示例 3:

输入: strs = ["a"]

输出: [["a"]]

提示:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] 仅包含小写字母

解法

思路:

互为字母异位词的单词他们字母出现的类型和次数都是一致的,可以将所有字母进行排序,在构建一个哈希表,将排序后的字符串作为键,如果表中有该键,则添加到对应的值列表中,如果没有,则添加一对字母-列表的键值对,并将字符串添加到列表中。也可以统计字母出现的次数,将字母和次数作为键进行存储,例如teaa1e1t1存储到哈希表中。

代码:

//一、排序后作为键
import java.util.*;
public class leetcode_002 {
    public static List<List<String>> groupAnagrams(String[] strs) {
        //创建一个map,用以存放排序后的字符串,和对应的字符串列表
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String key = new String(chars);
            if(map.containsKey(key)) {
                map.get(key).add(str);
                continue;
            }
            map.put(key, new ArrayList<>());
            map.get(key).add(str);
        }
        List<List<String>> res = new ArrayList<>();
        // 遍历Map并读取所有列表内容
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            res.add(entry.getValue());
        }
        return res;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] strList = str.split(" ");

        List<List<String>> res = new ArrayList<>();
        res=groupAnagrams(strList);
        System.out.println(res);

    }
}

//二、将字母和出现的次数作为键
import java.util.*;
public class leetcode_002 {
    public static List<List<String>> groupAnagrams(String[] strs) {
        //创建一个map,用以存放排序后的字符串,和对应的字符串列表
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            int[] counts = new int[26];
            for(int i = 0; i < str.length(); i++) {
                counts[str.charAt(i) - 'a']++;
            }
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < 26; i++) {
                if(counts[i]!=0) {
                    sb.append((char)(i + 'a'));
                    sb.append(counts[i]);
                }
            }
            String key = sb.toString();
            if(map.containsKey(key)) {
                map.get(key).add(str);
                continue;
            }

            map.put(key, new ArrayList<>());
            map.get(key).add(str);
        }
        List<List<String>> res = new ArrayList<>();
        // 遍历Map并读取所有列表内容
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            res.add(entry.getValue());
        }
        return res;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] strList = str.split(" ");

        List<List<String>> res = new ArrayList<>();
        res=groupAnagrams(strList);
        System.out.println(res);

    }
}
posted @ 2025-09-05 14:56  狐狸胡兔  阅读(10)  评论(0)    收藏  举报