https://leetcode-cn.com/problems/group-anagrams/
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
示例:
输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出:
[
[“ate”,“eat”,“tea”],
[“nat”,“tan”],
[“bat”]
]
说明:
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> resultList = new ArrayList<>();
if(strs == null || strs.length == 0){
return resultList;
}
if(strs.length == 1){
List<String> sList = new ArrayList<>();
sList.add(strs[0]);
resultList.add(sList);
return resultList;
}
Map<Long,List<String>> longMap = new HashMap<>();
List<String> nullList = new ArrayList<>();
for(String str : strs){
if(str.length() == 0){
nullList.add(str);
continue;
}
BitSet bitset = new BitSet();
char[] cs = str.toCharArray();
for(char c : cs){
bitset.set(c - 64);
}
long[] longs = bitset.toLongArray();
List<String> sList = longMap.get(longs[0]);
if(sList == null){
sList = new ArrayList<String>();
longMap.put(longs[0], sList);
resultList.add(sList);
}
sList.add(str);
}
if(nullList.size() > 0){
resultList.add(nullList);
}
return resultList;
}
}
O(N*K)
O(N*K)
Ascii码-'a'
得到的数字下标对应的素数的乘积class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> resultList = new ArrayList<>();
if(strs == null || strs.length == 0){
return resultList;
}
if(strs.length == 1){
List<String> sList = new ArrayList<>();
sList.add(strs[0]);
resultList.add(sList);
return resultList;
}
Map<Long,List<String>> longMap = new HashMap<>();
List<String> nullList = new ArrayList<>();
int[] primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
for(String str : strs){
if(str.length() == 0){
nullList.add(str);
continue;
}
char[] cs = str.toCharArray();
long product = 1L;
for(char c : cs){
product = product * primes[(c - 'a')];
}
List<String> sList = longMap.get(product);
if(sList == null){
sList = new ArrayList<String>();
longMap.put(product, sList);
resultList.add(sList);
}
sList.add(str);
}
if(nullList.size() > 0){
resultList.add(nullList);
}
return resultList;
}
}
O(N*K)
将每个单词先排序,然后跟HashMap中记录比对。
这种方法时间复杂度为O(N*K*logK)
,空间复杂度依然是O(N*K)
。
将字符串中每个字符串统计,并使用分隔符做HashMap的Key。如:abbc,则转换后为a1#b2#c1
。