-
Notifications
You must be signed in to change notification settings - Fork 1
/
anagrams.cpp
70 lines (53 loc) · 1.46 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
// Anagrams
// Jeff Ruhl
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <regex>
#include <set>
using namespace std;
#define FILE_END "<end>"
typedef multiset<char> CharMultiSet;
bool is_anagram( string, string );
int main( int argc, char *argv[] ) {
ifstream in("anagrams.in");
if ( in ) {
ofstream out("anagrams.out");
string line1, line2;
while ( getline( in, line1 ) && FILE_END != line1 && getline( in, line2 ) && FILE_END != line2 ) {
if ( is_anagram( line1, line2 ) ) {
out << "YES" << endl;
}
else {
out << "NO" << endl;
}
}
out.close();
in.close();
}
else {
cout << "File 'anagrams.in' not found" << endl;
}
return 0;
}
bool is_anagram( string phrase1, string phrase2 ) {
CharMultiSet phrase1_set, phrase2_set;
auto clean_string = []( const string& str ) -> string {
string result;
regex irrelevant_chars("[^a-zA-Z]");
result = regex_replace( str, irrelevant_chars, string("") );
transform( result.begin(), result.end(), result.begin(), ::tolower );
return result;
};
auto fill_set = []( const string& phrase, CharMultiSet& set ) {
for_each( phrase.begin(), phrase.end(), [&]( char c ) {
set.insert( c );
});
};
phrase1 = clean_string( phrase1 );
phrase2 = clean_string( phrase2 );
fill_set( phrase1, phrase1_set );
fill_set( phrase2, phrase2_set );
return phrase1_set == phrase2_set;
}