lurenaa的博客

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
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs)
{
map<string, vector<string>> mp;
for(auto str: strs) {
string s = respell(str);
mp[s].push_back(str);
}
vector<vector<string>> vs;
for(auto v : mp)
vs.push_back(v.second);
return vs;
}

string respell(string s) {
vector<char> vec;
for(auto i : s) {
vec.push_back(i);
}
sort(vec.begin(), vec.end());
string ns = "";
for(auto x : vec) {
ns += x;
}
return ns;
}
};

Accepted

101/101 cases passed (64 ms)

Your runtime beats 47.5 % of cpp submissions

Your memory usage beats 11.54 % of cpp submissions (22.7 MB)