Group Anagrams
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Example 1:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = ["a"]
Output: [["a"]]
Solution
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
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
vector<map<char, int>> myVec;
for (auto i : strs) {
map<char, int> tmp;
for (auto j : i) {
tmp[j]++;
}
myVec.push_back(tmp);
}
set<map<char, int>> mySet;
for (int i = 0; i < strs.size(); i++) {
if (mySet.count(myVec[i]) == 0) {
vector<string> tmp;
tmp.push_back(strs[i]);
for (int j = i + 1; j < strs.size(); j++) {
if (myVec[i] == myVec[j]) {
tmp.push_back(strs[j]);
}
}
res.push_back(tmp);
mySet.insert(myVec[i]);
}
}
return res;
}
};