-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGroup_Anagrams.cpp
More file actions
36 lines (26 loc) · 909 Bytes
/
Group_Anagrams.cpp
File metadata and controls
36 lines (26 loc) · 909 Bytes
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
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>>ans;
unordered_map<string, vector<string>>mp;
/*
Consider example 1 : strs = ["eat","tea","tan","ate","nat","bat"]
After the below opeartion of for loop map will contain
aet -- eat, tea, ate
ant -- tan, nat
abt -- bat
*/
for(int i = 0 ; i < strs.size() ; i++)
{
string s = strs[i];
sort(strs[i].begin(),strs[i].end());
mp[strs[i]].push_back(s);
}
//now simply put the elements of second column of map in ans
for(auto i : mp)
{
ans.push_back(i.second);
}
return ans;
}
};