-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathM0020.cpp
55 lines (43 loc) · 925 Bytes
/
M0020.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
/*
Problem Code: https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem
*/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
int most_common(unordered_map<char, int> &freq) {
int max_freq, max_cnt = 0;
unordered_map<int, int> count;
for (auto it: freq)
count[it.second]++;
for (auto it: count)
if (it.second > max_cnt) {
max_cnt = it.second;
max_freq = it.first;
}
return max_freq;
}
string isValid(string &s) {
unordered_map<char, int> freq;
for (char c: s)
freq[c]++;
bool flag = false;
int max_freq = most_common(freq);
for (auto it: freq) {
if (it.second == max_freq)
continue;
else if (!flag && (max_freq != 1 && it.second == 1 || it.second - 1 == max_freq))
flag = true;
else
return "NO";
}
return "YES";
}
int main() {
string s;
cin >> s;
cout << isValid(s);
return 0;
}