-
Notifications
You must be signed in to change notification settings - Fork 26
/
group-anagrams.cpp
125 lines (85 loc) · 2.38 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma GCC optimize("Ofast")
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <numeric>
#include <iterator>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
void abhisheknaiidu()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
// S.C - O(w*n + n) ==> O(w*n)
// w = length of words vector and n = longest anagram length;
// T.C - O(w*nlog(n) + n*wlog(w));
// sorting w words of max of length n;
// sorting w indices with anagrams of max length n;
int main(int argc, char* argv[]) {
abhisheknaiidu();
vector <string> words{"yo", "act", "flop", "tac", "cat", "oy", "olfp"};
vector <string> sortedWords;
for(auto word: words) {
sort(word.begin(), word.end());
sortedWords.push_back(word);
}
// vector<int> indices(words.size());
// iota(indices.begin(), indices.end(), 0);
// sort(indices.begin(), indices.end(), [sortedWords](int a, int b) -> bool {
// // cout << a << " " << b << endl;
// return sortedWords[a] < sortedWords[b];
// });
// vector<vector<string>> res;
// vector<string> anaGroup;
// string currentAna = sortedWords[indices[0]];
// for(auto index: indices) {
// string word = words[index];
// string sortedWord = sortedWords[index];
// if(sortedWord == currentAna) {
// anaGroup.push_back(word);
// continue;
// }
// res.push_back(anaGroup);
// anaGroup = vector <string>{word};
// currentAna = sortedWord;
// }
// res.push_back(anaGroup);
// MOST OPTIMAL 🔥
// S.C remains same
// T.C will be O(w*nlogn)
// because we are just sorting w words of max of length n;
unordered_map<string, vector<string>>m;
for(auto word: words) {
string sortedWord = word;
sort(sortedWord.begin(), sortedWord.end());
if(m.find(sortedWord) != m.end()) {
m[sortedWord].push_back(word);
}
else {
m[sortedWord] = vector<string>{word};
}
}
vector<vector<string>> res;
for(auto x:m) {
res.push_back(x.second);
}
for(auto x: res) {
for(auto y: x) {
cout << y << " ";
}
cout << endl;
}
return 0;
}