-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsherlock_anagrams1.cpp
More file actions
95 lines (66 loc) · 2.35 KB
/
sherlock_anagrams1.cpp
File metadata and controls
95 lines (66 loc) · 2.35 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
81
82
83
84
85
86
87
88
89
90
91
92
// Original Link
// https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
/////////////////// SOLUTION ////////////////////
unordered_map< string, unordered_map<char, unsigned int> > data;
bool is_equal(const unordered_map<char, unsigned int>* a, const unordered_map<char, unsigned int>* b)
{
for(auto i=a->begin(); i!=a->end(); ++i)
{
const auto j = b->find(i->first);
if( (j==b->end()) || (i->second != j->second) ) return false;
}
return true;
}
unordered_map<char, unsigned int>* dict(const string& s)
{
unordered_map<char, unsigned int> res;
auto temp = data.find(s);
if(temp!=data.end()) return &(temp->second);
// Compute
for(const auto& e : s) if(res.find(e)==res.end()) res[e]=1; else res[e]++;
data[s] = res;
return &(data[s]);
}
// Complete the sherlockAndAnagrams function below.
// NOTES
// - Substrings Computation is O(N^2) in String Size as there are 2 DoF both dependning on N : the 1) Substring Size and the 2) Substring Starting Pos
// - In this case, Substrings pairs of the same size need to be computed
// - The best representation to perform anagram checking is as unordered set of characters hence using unordered_map to associtate to each char the number of times it is observed
// - Comparing 2 dictionaries is easy : just iterate over one's keys and compare the related value with the same key value in the othe dictionary
int sherlockAndAnagrams(string s)
{
int res=0;
// String -> ValidSizes(Int)
for(unsigned int n=1; n<s.size(); ++n)
{
for(unsigned int i=0; i<s.size()-1; ++i)
{
for(unsigned int j=i+1; j<s.size(); ++j)
{
const auto d1 = dict(s.substr(i,n));
const auto d2 = dict(s.substr(j,n));
if(is_equal(d1,d2)) res++;
}
}
}
return res;
}
/////////////////////////////////////////////////////////////
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int q_itr = 0; q_itr < q; q_itr++) {
string s;
getline(cin, s);
int result = sherlockAndAnagrams(s);
fout << result << "\n";
}
fout.close();
return 0;
}