-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_anagrams.cpp
More file actions
80 lines (65 loc) · 2.09 KB
/
group_anagrams.cpp
File metadata and controls
80 lines (65 loc) · 2.09 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
49. Group Anagrams
Given an array of strings strs, group the anagrams together.
You can return the answer in any order.
Time Complexity: O(n * k * log(k)) where n is the number of strings and k is the maximum length of a string
Space Complexity: O(n * k) for storing all strings in the hash map
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> anagram_groups;
for (const string& s : strs) {
string sorted_str = s;
sort(sorted_str.begin(), sorted_str.end());
anagram_groups[sorted_str].push_back(s);
}
vector<vector<string>> result;
for (auto& pair : anagram_groups) {
result.push_back(pair.second);
}
return result;
}
};
// Helper function to print vector of vector of strings
void printResult(const vector<vector<string>>& result) {
cout << "[";
for (size_t i = 0; i < result.size(); i++) {
cout << "[";
for (size_t j = 0; j < result[i].size(); j++) {
cout << "\"" << result[i][j] << "\"";
if (j < result[i].size() - 1) cout << ",";
}
cout << "]";
if (i < result.size() - 1) cout << ",";
}
cout << "]" << endl;
}
int main() {
Solution solution;
// Test case 1
vector<string> strs1 = {"eat", "tea", "tan", "ate", "nat", "bat"};
cout << "Input: [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]" << endl;
cout << "Output: ";
printResult(solution.groupAnagrams(strs1));
cout << endl;
// Test case 2
vector<string> strs2 = {""};
cout << "Input: [\"\"]" << endl;
cout << "Output: ";
printResult(solution.groupAnagrams(strs2));
cout << endl;
// Test case 3
vector<string> strs3 = {"a"};
cout << "Input: [\"a\"]" << endl;
cout << "Output: ";
printResult(solution.groupAnagrams(strs3));
cout << endl;
return 0;
}