-
Notifications
You must be signed in to change notification settings - Fork 12
/
SherlockAndTheValidString.cpp
77 lines (62 loc) · 1.52 KB
/
SherlockAndTheValidString.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
#include <bits/stdc++.h>
using namespace std;
string isValid(string s){
// Complete this function
map<char, int> m;
int n = s.size();
for(int i = 0; i < n; i++){
if(m.find(s[i]) == m.end()){
m[s[i]] = 1;
}
else{
m[s[i]]++;
}
}
map<char, int> :: iterator it = m.begin();
int first = -1, second = -1, firstCount = 0, secondCount = 0;
while(it != m.end()){
//cout << it->first << " " << it->second << endl;
if(first == -1){
first = it->second;
firstCount++;
}
else if(it->second == first){
firstCount++;
}
else if(second == -1){
second = it->second;
secondCount++;
}
else if(it->second == second){
secondCount++;
}
else{
return "NO";
}
it++;
}
/*
cout << endl;
cout << "First : " << first << " ,Second : " << second << " ,firstCount" << firstCount << " ,secondCount" << secondCount;
cout << endl;
*/
if(first == -1 || second == -1){
return "YES";
}
if(abs(first - second) == 1){
if(firstCount == 1 || secondCount == 1){
return "YES";
}
}
if(second == 1 && secondCount == 1){
return "YES";
}
return "NO";
}
int main() {
string s;
cin >> s;
string result = isValid(s);
cout << result << endl;
return 0;
}