-
Notifications
You must be signed in to change notification settings - Fork 0
/
49.Group Anagrams.cpp
41 lines (27 loc) · 963 Bytes
/
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
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> Result;
map<string,int> OrderList;
map<string,int> StringPos;
int pos;
sort(strs.begin(),strs.end());
for(int i = 0 ; i< strs.size();i++){
string tmp = strs[i];
sort(tmp.begin(),tmp.end());
if(OrderList[tmp] == 0)
pos=-1;
else
pos = StringPos[tmp];
if(pos == -1){
vector<string> ResultElement;
ResultElement.push_back(strs[i]);
Result.push_back(ResultElement);
OrderList[tmp] = 1;
StringPos[tmp] = Result.size()-1;
}else
Result[pos].push_back(strs[i]);
}
return Result;
}
};