-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
HappyLadybugs.java
51 lines (46 loc) · 1.29 KB
/
HappyLadybugs.java
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Q = in.nextInt();
for(int a0 = 0; a0 < Q; a0++){
int n = in.nextInt();
char b[] = in.next().toCharArray();
char a[] = new char[26];
int space=0;
String ans = "NO";
for(int i=0;i<n;i++){
if(b[i]=='_')
space++;
else
a[b[i]-65]++;
}
if(space==0&&ordered(n,b))
ans="YES";
else if(space>0){
for(int i=0;i<26;i++){
if(a[i]==1)
break;
if(i==25)
ans="YES";
}
}
System.out.println(ans);
}
}
public static boolean ordered(int n, char b[]) {
if(n==1&&b[0]!='_')
return false;
if(b[0]!=b[1] || b[n-2]!=b[n-1])
return false;
for(int i=1;i<n-1;i++) {
if(b[i]!=b[i-1]&&b[i]!=b[i+1])
return false;
}
return true;
}
}