-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.cpp
71 lines (54 loc) · 1.58 KB
/
solution.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
#include <bits/stdc++.h>
using namespace std;
// Implementation Part
string happyLadybugs(string b) {
vector<int> freq(26, 0);
int count = 0;
// counting frequency of empty place and colors
for(int i = 0; i < b.size(); i++){
if(b[i] == '_')
count++;
else{
int index = b[i] - 'A';
freq[index]++;
}
}
// if any color has frequency 1 then it is not possible to get same color
// at its adjacent positions (two positions)
for(int i = 0; i < freq.size(); i++){
if(freq[i] == 1)
return "NO";
}
// if there is empty space then we can shift positions
// as there is no color with frequency = 1, so we can get adjacent colors
if(count != 0)
return "YES";
else {
// if there is no empty place then, we need to check
// that current adjacents have same color,
// as there is no empty place so shifting is not possible
for(int i = 1; i < (b.size() - 1); i++){
if(b[i] != b[i + 1] && b[i] != b[ i - 1])
return "NO";
}
return "YES";
}
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int g;
cin >> g;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int g_itr = 0; g_itr < g; g_itr++) {
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string b;
getline(cin, b);
string result = happyLadybugs(b);
fout << result << "\n";
}
fout.close();
return 0;
}