-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValeraAndX.java
More file actions
71 lines (70 loc) · 2.28 KB
/
ValeraAndX.java
File metadata and controls
71 lines (70 loc) · 2.28 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
public class ValeraAndX {
public static void main(String[] args) {
String[] original = getInput();
if(original.length != 0) examineThreeParts(original, original[0].length());
}
private static String[] getInput() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
if(s.charAt(0) == s.charAt(1)) {
sc.close();
System.out.print("NO");
return new String[0];
}
String[] original = new String[n];
original[0] = s;
int index = 1;
while(sc.hasNextLine()) {
original[index] = sc.nextLine();
index++;
}
sc.close();
return original;
}
private static void examineThreeParts(String[] original, int n) {
char x = original[0].charAt(0), y = original[0].charAt(1);
// the first n / 2 - 1 strings to be examined
for(int i = 0, j = n - 1; i != j; i++, j--)
if(!examineOneString(original[i], i, j, x, y)) return ;
// the middle string to be examined
for(int i = 0; i < n; i++) {
char c = original[n / 2].charAt(i);
if(i == n / 2) {
if( c != x) {
System.out.print("NO");
return ;
}
} else {
if(c != y) {
System.out.print("NO");
return ;
}
}
}
// the last n / 2 - 1 strings to be examined
for(int i = n / 2 - 1, j = n / 2 + 1; i >= 0; i--, j++)
if(!examineOneString(original[j], i, j, x, y)) return ;
// all clear
System.out.print("YES");
}
private static boolean examineOneString(String string, int i, int j, char x, char y) {
for(int k = 0; k < string.length(); k++) {
char c = string.charAt(k);
if(k == i || k == j) {
if(c != x) {
System.out.print("NO");
return false;
}
} else {
if(c != y) {
System.out.print("NO");
return false;
}
}
}
return true;
}
}