Leetcode 49.字母异位词分组

2023-02-14,,,,

字母异位分组

给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

示例:

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

输出:

[

["ate","eat","tea"],

["nat","tan"],

["bat"]

]

说明:

所有输入均为小写字母。
不考虑答案输出的顺序。

方法一:排序数组分类

思路

当且仅当它们的排序字符串相等时,两个字符串是字母异位词。

算法

维护一个映射 ans : {String -> List},其中每个键 K\text{K}K 是一个排序字符串,每个值是初始输入的字符串列表,排序后等于 K\text{K}K。

在 Java 中,我们将键存储为字符串,例如,code。 在 Python 中,我们将键存储为散列化元组,例如,('c', 'o', 'd', 'e')。

 class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs.length == 0) return new ArrayList();
Map<String, List> ans = new HashMap<String, List>();
for (String s : strs) {
char[] ca = s.toCharArray();
Arrays.sort(ca);
String key = String.valueOf(ca);
if (!ans.containsKey(key)) ans.put(key, new ArrayList());
ans.get(key).add(s);
}
return new ArrayList(ans.values());
}
}

方法二:按计数分类

思路

当且仅当它们的字符计数(每个字符的出现次数)相同时,两个字符串是字母异位词。

算法

我们可以将每个字符串 s\text{s}s 转换为字符数 count\text{count}count,由26个非负整数组成,表示 a\text{a}a,b\text{b}b,c\text{c}c 的数量等。我们使用这些计数作为哈希映射的基础。

在 Java 中,我们的字符数 count 的散列化表示将是一个用 **#** 字符分隔的字符串。 例如,abbccc 将表示为 #1#2#3#0#0#0 ...#0,其中总共有26个条目。 在 python 中,表示将是一个计数的元组。 例如,abbccc 将表示为 (1,2,3,0,0,...,0),其中总共有 26 个条目。

 class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs.length == 0) return new ArrayList();
Map<String, List> ans = new HashMap<String, List>();
int[] count = new int[26];
for (String s : strs) {
Arrays.fill(count, 0);
for (char c : s.toCharArray()) count[c - 'a']++; StringBuilder sb = new StringBuilder("");
for (int i = 0; i < 26; i++) {
sb.append('#');
sb.append(count[i]);
}
String key = sb.toString();
if (!ans.containsKey(key)) ans.put(key, new ArrayList());
ans.get(key).add(s);
}
return new ArrayList(ans.values());
}
}

Leetcode 49.字母异位词分组的相关教程结束。

《Leetcode 49.字母异位词分组.doc》

下载本文的Word格式文档,以方便收藏与打印。