-
Notifications
You must be signed in to change notification settings - Fork 0
/
ganag.cpp
74 lines (61 loc) · 1.74 KB
/
ganag.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
//
// ganag.cpp
// xcode
//
// Created by Gokul Nadathur on 8/29/16.
// Copyright © 2016 Gokul Nadathur. All rights reserved.
//
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
class Solution {
public:
unordered_map<string, unordered_map<char, int>> _check;
void generateMap(unordered_map<char, int>& outp, string& s) {
for (auto c : s) {
outp[c] ++;
}
}
bool theSame(unordered_map<char, int>& a, unordered_map<char, int>& b)
{
for (auto it = a.begin(); it != a.end(); ++it) {
auto k = it->first;
auto dit = b.find(k);
if (dit == b.end() || dit->second != it->second) {
return false;
}
}
return true;
}
void findPlace(string& s, unordered_map<string, vector<string>>& res) {
unordered_map<char, int> gen;
generateMap(gen, s);
for (auto it = _check.begin(); it != _check.end(); ++it) {
if (theSame(gen, it->second)) {
res[it->first].push_back(s);
return;
}
}
res[s] = { s };
_check[s] = gen;
}
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> res;
for (auto& s : strs) {
findPlace(s, res);
}
vector<vector<string>> fres;
for (auto it = res.begin(); it != res.end(); ++it) {
fres.push_back(it->second);
}
return fres;
}
};
int main(int argc, char** argv)
{
vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
Solution s;
s.groupAnagrams(strs);
return 0;
}