-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromePreviousOrNext.java
54 lines (45 loc) · 1.39 KB
/
PalindromePreviousOrNext.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
52
53
54
import java.util.Scanner;
class PalindromeTransformation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String S = sc.next();
sc.close();
if (canBePalindrome(S)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
private static boolean canBePalindrome(String S) {
int len = S.length();
for (int i = 0; i < len / 2; i++) {
char left = S.charAt(i);
char right = S.charAt(len - i - 1);
if (!canMatch(left, right)) {
return false;
}
}
return true;
}
private static boolean canMatch(char left, char right) {
if (left == right) {
return true;
}
if ((left == 'A' && right == 'B') || (left == 'B' && right == 'A')) {
return true;
}
if ((left == 'Z' && right == 'Y') || (left == 'Y' && right == 'Z')) {
return true;
}
if ((left == 'a' && right == 'b') || (left == 'b' && right == 'a')) {
return true;
}
if ((left == 'z' && right == 'y') || (left == 'y' && right == 'z')) {
return true;
}
if (Math.abs(left - right) <= 2) {
return true;
}
return false;
}
}