-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path49_group_anagrams.cpp
More file actions
55 lines (49 loc) · 1.46 KB
/
49_group_anagrams.cpp
File metadata and controls
55 lines (49 loc) · 1.46 KB
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
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <iomanip>
#include <chrono>
class Solution
{
public:
std::vector<std::vector<std::string>> groupAnagrams(std::vector<std::string> &strs)
{
std::unordered_map<std::string, std::vector<std::string>> anagrams{};
for (auto str : strs)
{
std::string x = str;
std::sort(x.begin(), x.end());
anagrams[x].push_back(str);
}
std::vector<std::vector<std::string>> res;
for (auto anagram : anagrams)
{
res.push_back(anagram.second);
}
return res;
}
};
int main()
{
auto start = std::chrono::high_resolution_clock::now();
Solution s;
std::vector<std::string> strs{"eat", "tea", "tan", "ate", "nat", "bat"};
int extraCandies = 3;
std::vector<std::vector<std::string>> res = s.groupAnagrams(strs);
// std::cout << res << std::endl;
for (auto a : res)
{
copy(a.begin(), a.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << "\n";
}
auto end = std::chrono::high_resolution_clock::now();
double time_taken =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
time_taken *= 1e-9;
std::cout << "Time taken by program is : " << std::fixed
<< time_taken << std::setprecision(9);
std::cout << " sec" << std::endl;
return 0;
}