-
Notifications
You must be signed in to change notification settings - Fork 0
/
charFreqDistribution.cpp
59 lines (57 loc) · 1.63 KB
/
charFreqDistribution.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
#include <iostream>
#include <map>
#include <set>
#include <climits>
using namespace std;
map<char, int> parseString(string input) {
map<char, int> output;
for (unsigned int i=0; i < input.length(); i++) {
if (output.find(input[i]) == output.end()) {
output[input[i]] = 1;
} else {
output[input[i]] += 1;
}
}
return output;
}
int main(int argc, char *argv[]){
string input;
getline(cin, input);
map<char, int> frequency = parseString(input);
set<char> mostCommon;
set<char> leastCommon;
int mcSoFar = 0;
int lcSoFar = INT_MAX;
for (auto i = frequency.begin(); i != frequency.end(); i++) {
cout << i->first << " frequency " << i->second << endl;
if (i->second >= mcSoFar) {
if (i->second > mcSoFar) {
mostCommon.clear();
mostCommon.insert(i->first);
mcSoFar = i->second;
} else {
mostCommon.insert(i->first);
}
}
if (i->second <= lcSoFar) {
if (i->second < lcSoFar) {
leastCommon.clear();
leastCommon.insert(i->first);
lcSoFar = i->second;
} else {
leastCommon.insert(i->first);
}
}
}
cout << "Most Common characters : ";
for (auto i = mostCommon.begin(); i != mostCommon.end(); i++) {
cout << *i << " ";
}
cout << endl;
cout << "Least Common characters : ";
for (auto i = leastCommon.begin(); i != leastCommon.end(); i++) {
cout << *i << " ";
}
cout << endl;
return 0;
}