forked from super30admin/Hashing-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup Anagrams.cpp
57 lines (54 loc) · 1.98 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Time Complexity: O(n * klogk)
// Space complexity: O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no
// TC = O(nk) - creating own hashing function
// class Solution {
// public:
// vector<vector<string>> groupAnagrams(vector<string>& strs) {
// vector<vector<string>> res;
// unordered_map <double, vector<string>> map;
// for(string s: strs){
// // calculating hash of each string through primeProduct
// double p = primeProduct(s);
// map[p].push_back(s);
// }
// for(auto i: map) {
// res.push_back(i.second);
// }
// return res;
// }
// double primeProduct(string s) {
// vector<int> primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,67,71,73,79,83,89,97,101,103};
// double mult = 1;
// for(int i = 0; i < s.length(); i++){
// char c = s.at(i);
// mult = mult*(primes[c - 'a']);
// }
// return mult;
// }
// };
// hashmap to map keys which contains the sorted characters as keys and then add the string as values List<String>() to the keys
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
if(strs.size() == 0) return res;
unordered_map<string, vector<string>> map;
/* 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(string str: strs) {
string sorted = str;
sort(sorted.begin(), sorted.end());
// push_back as vector
map[sorted].push_back(str);
}
// important, i.second, like pair<string, vector<string>> p;
for(auto i: map) res.push_back(i.second);
return res;
}
};