[LeetCode] Group Anagrams

简介: The function signature has been updated to return a more intuitive vectorwhich treats a single string as a group of anagrams consisting of only itself.

The function signature has been updated to return a more intuitive vector<vector<string>>which treats a single string as a group of anagrams consisting of only itself.

The idea is to use an unordered_map to store those strings that are anagrams. We use the sorted string as the key and the string itself as the value. The strings are stored in a multisetsince there may be duplicates. Moreover, multiset will sort them by default as we desire.

The code is as follows.

 1 class Solution {
 2 public:
 3     vector<vector<string>> groupAnagrams(vector<string>& strs) {
 4         unordered_map<string, multiset<string>> mp;
 5         for (string s : strs) {
 6             string t = s; 
 7             sort(t.begin(), t.end());
 8             mp[t].insert(s);
 9         }
10         vector<vector<string>> anagrams;
11         for (auto m : mp) { 
12             vector<string> anagram(m.second.begin(), m.second.end());
13             anagrams.push_back(anagram);
14         }
15         return anagrams;
16     }
17 };

Update: general sort takes O(nlogn) time. In this problem, since the string only contains lower-case alphabets, we can write a sorting function using counting sort (O(n) time) to speed up the sorting process. I write a string sorting functionstrSort below and using it to sort the string achieves the overall running time 72ms for this problem while the above code takes 76ms.

 1 class Solution {
 2 public:
 3     vector<vector<string>> groupAnagrams(vector<string>& strs) {
 4         unordered_map<string, multiset<string>> mp;
 5         for (string s : strs) {
 6             string t = strSort(s);
 7             mp[t].insert(s);
 8         }
 9         vector<vector<string>> anagrams;
10         for (auto m : mp) { 
11             vector<string> anagram(m.second.begin(), m.second.end());
12             anagrams.push_back(anagram);
13         }
14         return anagrams;
15     }
16 private:
17     string strSort(string& s) {
18         int count[26] = {0}, n = s.length();
19         for (int i = 0; i < n; i++)
20             count[s[i] - 'a']++;
21         int p = 0;
22         string t(n, 'a');
23         for (int j = 0; j < 26; j++)
24             for (int i = 0; i < count[j]; i++)
25                 t[p++] += j;
26         return t;
27     } 
28 };

 

目录
相关文章
LeetCode 438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.
90 0
LeetCode 438. Find All Anagrams in a String
|
C语言 Python
LeetCode 49. Group Anagrams
给定一组字符串,将由相同字母组成的字符串组合在一起。 注意:所有给定的输入都是小写,输出的顺序不重要
76 0
|
存储 Java 索引
LeetCode 49: 字母异位词分组 Group Anagrams
# LeetCode 49: 字母异位词分组 Group Anagrams ### 题目: 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 Given an array of strings, group anagrams together.
738 0
|
存储 测试技术
(转)leetcode:Find All Anagrams in a String 滑动窗口方法总结
今天做了几道滑动窗口的题,稍微总结一下。 起因源于早上在leetcode上pick one,随机到了一个easy的题目,想着随便做了,结果半天也找不到最优解,耗时300多ms,A是A了,不过就是暴力罢了。
1703 0
LeetCode - 49. Group Anagrams
49. Group Anagrams  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个由string类型构成的集合,让你按照每个字符串的单词构成集合来将这个集合分类.
983 0
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行