-
Notifications
You must be signed in to change notification settings - Fork 1
/
HappyLadybugs.cpp
102 lines (79 loc) · 2.1 KB
/
HappyLadybugs.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// problem: "https://www.hackerrank.com/challenges/happy-ladybugs/problem"
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
int g,i,j,u,k,t,c,n;
char string[100],ch;
cin>>g;
for(i=0;i<g;i++)
{
cin>>n;
cin>>string;
t=u=0;
for(j=0;j<n;j++)
if(string[j]=='_')
u++;
if(u==0)
{
if(string[0]!=string[1])
{
cout<<"NO"<<endl;
continue;
}
else if(string[n-2]!=string[n-1])
{
cout<<"NO"<<endl;
continue;
}
else
{
for(j=1;j<n-1;j++)
{
if((string[j]!=string[j-1])&&(string[j]!=string[j+1]))
{
cout<<"NO"<<endl;
u=-1;
break;
}
}
if(u==-1)
continue;
else
{
cout<<"YES"<<endl;
continue;
}
}
}
else //u!=0
{
for(j=0;j<n;j++)
{
u=0;
if(isalpha(string[j]))
{
ch=string[j];
for(k=j;k<n;k++)
{
if(ch==string[k])
{
u++;
string[k]='1';
}
}
if(u==1)
{
cout<<"NO"<<endl;
t=-1;
break;
}
}
}
if(t==0)
cout<<"YES"<<endl;
}
}
return 0;
}