哈希-03-字母异位词分组

文章目录

1. 题目描述

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

示例 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] 仅包含小写字母

2. 思路及代码

错误示例1:

存在的问题:无法处理多个相同字符的情况,对空字符也处理不了。

java 复制代码
public List<List<String>> groupAnagrams(String[] strs) {
    Set<Set<Character>> sets = new HashSet<>();
    for (String str : strs) {
      char[] charArray = str.toCharArray();
      Set<Character> set = new HashSet<>();
      for (char c : charArray) {
        set.add(c);
      }
      sets.add(set);
    }
    List<List<String>> lists = new ArrayList<>();
    for (Set<Character> set : sets) {
      List<String> list = new ArrayList<>();
      for (String str : strs) {
        char[] charArray = str.toCharArray();
        int count = 0;
        for (char c : charArray) {
          if (set.contains(c)) {
            count ++;
          }
        }
        if (count == set.size()) {
          list.add(str);
        }
      }

      lists.add(list);
    }
    return lists;
}

错误示例2:

存在的问题:int[]没有重写equals方法,HashSet无法去重!!!会导致出现重复的分组

java 复制代码
public List<List<String>> groupAnagrams2(String[] strs) {
    HashSet<int[]> sets = new HashSet<>();
    for (String str : strs) {
      int[] ints = new int[26];
      char[] charArray = str.toCharArray();
      for (char c : charArray) {
        int ascii = c - 'a';
        ints[ascii] += 1;
      }
      sets.add(ints);
    }
    List<List<String>> lists = new ArrayList<>();
    for (int[] set : sets) {
      List<String> list = new ArrayList<>();
      for (String str : strs) {
        int[] ints = new int[26];
        char[] charArray = str.toCharArray();
        for (char c : charArray) {
          int ascii = c - 'a';
          ints[ascii] += 1;
        }
        if (set.equals(ints)) {
          list.add(str);
        }
      }

      lists.add(list);
    }
    return lists;
  }

正确示例:

  • 使用HashMap + List/Set处理一对多关系。
java 复制代码
public List<List<String>> groupAnagrams3(String[] strs) {

    Map<String, List<String>> map = new HashMap<>();
    for (String str : strs) {
      char[] charArray = str.toCharArray();
      Arrays.sort(charArray);
      //如果key不存在就创建新List,然后添加元素
      map.computeIfAbsent(String.valueOf(charArray), k -> new ArrayList<>()).add(str);
    }

    Collection<List<String>> values = map.values();
    return new ArrayList<>(values);
  }

总结

  1. HashSet无法对没有重写equals方法的数据结构进行去重。
  2. 异位词所涉及的字符不变,只是不同的组合,可以用一对多的数据结构来存储。

以上为个人学习分享,如有问题,欢迎指出:)

相关推荐
轻微的风格艾丝凡2 小时前
matlab推导QPR离散公式并验证
算法·matlab·谐振
岁岁的O泡奶2 小时前
NSSCTF_crypto_[SWPU 2020]happy
经验分享·python·算法·密码学
EchoL、3 小时前
【论文阅读】SteganoGAN:High Capacity Image Steganography with GANs
论文阅读·人工智能·笔记·算法
CoovallyAIHub3 小时前
深度学习驱动的视频异常检测(VAD),AI如何让监控更智能?
深度学习·算法·计算机视觉
于樱花森上飞舞3 小时前
【多线程】常见的锁策略与锁
java·开发语言·算法·java-ee
HUST3 小时前
C 语言 第八讲:VS实用调试技巧
运维·c语言·开发语言·数据结构·算法·c#
历程里程碑3 小时前
LeetCode128:哈希集合巧解最长连续序列
开发语言·数据结构·c++·算法·leetcode·哈希算法·散列表
@淡 定3 小时前
Hash 索引与 B+树索引的区别与适用场景
b树·算法·哈希算法
Tzarevich3 小时前
算法效率的核心:时间复杂度与空间复杂度
javascript·算法