-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAaagmnrs.h
64 lines (52 loc) · 1.34 KB
/
Aaagmnrs.h
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
#ifndef TOPCODER_AAAGMNRS_H_
#define TOPCODER_AAAGMNRS_H_
#include <algorithm>
#include <string>
#include <vector>
class Aaagmnrs
{
public:
std::vector <std::string> anagrams(std::vector <std::string> phrases);
private:
bool isAnagram(std::string s1, std::string s2);
void removeSpaces(std::string &s);
};
std::vector<std::string> Aaagmnrs::anagrams(std::vector<std::string> phrases)
{
std::vector<std::string> phrases2(phrases);
for (auto it = phrases2.begin(); it != phrases2.end(); ++it)
for (auto in = it + 1; in != phrases2.end(); )
{
if (isAnagram(*in, *it))
{
in = phrases2.erase(in);
}
else
{
++in;
}
}
return phrases2;
}
inline bool Aaagmnrs::isAnagram(std::string s1, std::string s2)
{
removeSpaces(s1);
removeSpaces(s2);
std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
std::transform(s2.begin(), s2.end(), s2.begin(), ::toupper);
std::sort(s1.begin(), s1.end());
std::sort(s2.begin(), s2.end());
return (s1.compare(s2) == 0);
}
inline void Aaagmnrs::removeSpaces(std::string & s)
{
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == ' ')
{
s.erase(i, 1);
--i;
}
}
}
#endif TOPCODER_AAAGMNRS_H_