forked from wahyuhjr/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGroup_anagram.cpp
More file actions
26 lines (22 loc) · 804 Bytes
/
Group_anagram.cpp
File metadata and controls
26 lines (22 loc) · 804 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
//it is a leetcode medium level important problem and this my efficient solution for this problem.
//hope u'll like it
//what is anagram?
//An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string,vector<string>>mp;
int i=0;
while(i<strs.size()){
string s=strs[i];
sort(s.begin(),s.end());
mp[s].push_back(strs[i]);
i++;
}
vector<vector<string>> ans;
for(auto i=mp.begin(); i!=mp.end(); i++){
ans.push_back(i->second);
}
return ans;
}
};