This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
anagrams.cpp
160 lines (143 loc) · 3.44 KB
/
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
// C++ code to print all anagrams together
#include <bits/stdc++.h>
using namespace std;
void solver(vector<string> my_list)
{
// Inner hashmap counts frequency
// of characters in a string.
// Outer hashmap for if same
// frequency characters are present in
// in a string then it will add it to
// the vector.
map<map<char, int>, vector<string> > my_map;
// Loop over all words
for(string str : my_list)
{
// Counting the frequency of the
// characters present in a string
map<char, int> temp_map;
vector<string> temp_my_list;
for(int i = 0; i < str.length(); ++i)
{
++temp_map[str[i]];
}
// If the same frequency of chanracters
// are alraedy present then add that
// string into that arraylist otherwise
// created a new arraylist and add that
// string
auto it = my_map.find(temp_map);
if (it != my_map.end())
{
it->second.push_back(str);
}
else
{
temp_my_list.push_back(str);
my_map.insert(make_pair(temp_map, temp_my_list));
}
}
// Stores the result in a vector
vector<vector<string> > result;
for(auto it = my_map.begin();it != my_map.end(); ++it)
{
result.push_back(it->second);
}
for(int i = 0; i < result.size(); ++i)
{
cout << "[";
for(int j = 0; j < result[i].size(); ++j)
{
cout << result[i][j] << ", ";
}
cout << "]";
}
}
// Driver code
int main()
{
vector<string> my_list= { "cat", "dog", "ogd", "god", "atc" };
solver(my_list);
return 0;
}
*/
// C++ program for finding all anagram
// pairs in the given array
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
// Utility function for
// printing anagram list
void printAnagram(
unordered_map<string,
vector<string> >& store)
{
unordered_map<string,
vector<string> >::iterator it;
for (it = store.begin();
it != store.end(); it++) {
vector<string> temp_vec(it->second);
int size = temp_vec.size();
if (size >= 1) {
for (int i = 0; i < size; i++) {
cout << temp_vec[i] << " ";
}
cout << "\n";
}
}
}
// Utility function for storing
// the vector of strings into HashMap
void storeInMap(vector<string>& vec)
{
unordered_map<string,
vector<string> >
store;
for (int i = 0; i < vec.size(); i++) {
string tempString(vec[i]);
sort(tempString.begin(),
tempString.end());
// Check for sorted string
// if it already exists
if (store.find(
tempString)
== store.end()) {
vector<string> temp_vec;
temp_vec.push_back(vec[i]);
store.insert(make_pair(
tempString, temp_vec));
}
else {
// Push new string to
// already existing key
vector<string> temp_vec(
store[tempString]);
temp_vec.push_back(vec[i]);
store[tempString] = temp_vec;
}
}
// print utility function for printing
// all the anagrams
printAnagram(store);
}
// Driver code
int main()
{
// initialize vector of strings
vector<string> arr;
arr.push_back("geeksquiz");
arr.push_back("geeksforgeeks");
arr.push_back("abcd");
arr.push_back("forgeeksgeeks");
arr.push_back("zuiqkeegs");
arr.push_back("cat");
arr.push_back("act");
arr.push_back("tca");
// utility function for storing
// strings into hashmap
storeInMap(arr);
return 0;
}