-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathhappy-ladybugs.cpp
63 lines (58 loc) · 1.32 KB
/
happy-ladybugs.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
/*
happy-ladybugs.cpp
Happy Ladybugs
My first submission got WA and I wrote something like this:
if (i + 1 < str.size() && isalpha(str[i + 1])) {
happy = true;
}
It's hard to describe what I was thinking at that time...
*/
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
int g;
cin >> g;
while (g--) {
int n;
cin >> n;
string str;
cin >> str;
int cnt[26];
memset(cnt, 0, sizeof(cnt));
bool hasEmptyCell = false;
bool hasUnhappyLadybugs = false;
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
cnt[str[i] - 'A']++;
bool happy = false;
if (i + 1 < str.size() && str[i + 1] == str[i]) {
happy = true;
}
if (i - 1 >= 0 && str[i - 1] == str[i]) {
happy = true;
}
if (!happy) {
hasUnhappyLadybugs = true;
}
} else {
hasEmptyCell = true;
}
}
if (!hasUnhappyLadybugs) {
cout << "YES" << endl;
} else {
if (!hasEmptyCell) {
cout << "NO" << endl;
} else {
bool canAdjust = true;
for (int i = 0; i < 26; i++) {
if (cnt[i] == 1) canAdjust = false;
}
cout << (canAdjust ? "YES" : "NO") << endl;
}
}
}
return 0;
}