-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49.group-anagrams.cpp
65 lines (61 loc) · 1.43 KB
/
49.group-anagrams.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* @lc app=leetcode id=49 lang=cpp
*
* [49] Group Anagrams
*
* https://leetcode.com/problems/group-anagrams/description/
*
* algorithms
* Medium (45.13%)
* Total Accepted: 304.3K
* Total Submissions: 674.1K
* Testcase Example: '["eat","tea","tan","ate","nat","bat"]'
*
* Given an array of strings, group anagrams together.
*
* Example:
*
*
* Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
* Output:
* [
* ["ate","eat","tea"],
* ["nat","tan"],
* ["bat"]
* ]
*
* Note:
*
*
* All inputs will be in lowercase.
* The order of your output does not matter.
*
*
*/
#if 1 //95.85%
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
vector<size_t> sortedHash;
sortedHash.reserve(strs.size());
map<size_t, int>hashIndex;
hash<string> hashFunc;
for(string str : strs){
sort(str.begin(), str.end());
sortedHash.push_back(hashFunc(str));
}
for(int i = 0 ; i < strs.size(); ++i){
auto found = hashIndex.find(sortedHash[i]);
if(found != hashIndex.end()){
result[found->second].push_back(strs[i]);
}
else{
hashIndex[sortedHash[i]] = result.size();
result.push_back(vector<string>(1,strs[i]));
}
}
return result;
}
};
#endif