-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54.cpp
More file actions
47 lines (42 loc) · 1.03 KB
/
54.cpp
File metadata and controls
47 lines (42 loc) · 1.03 KB
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
string isValid(string s) {
int count[26] = {0};
for(int i=0;i<s.length();i++) {
count[s[i]-'a']++;
}
int first = -1;
int first_count = 0;
int second = -1;
int second_count = 0;
for(int i=0;i<26;i++) {
if(count[i]!=0) {
if(first==-1||first==count[i]) {
first = count[i];
first_count++;
} else if(second==-1||second==count[i]) {
second = count[i];
second_count++;
} else {
return "NO";
}
}
}
if(second==-1) {
return "YES";
} else if(first>second) {
if(first_count==1&&first-second==1) {
return "YES";
} else if(second_count==1) {
return "YES";
} else {
return "NO";
}
} else {
if(second_count==1&&second-first==1) {
return "YES";
} else if(first_count==1) {
return "YES";
} else {
return "NO";
}
}
}