-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.java
61 lines (49 loc) · 1.49 KB
/
Solution.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
55
56
57
58
59
60
61
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the palindromeIndex function below.
static int palindromeIndex(String s) {
if (isPalindrom(s)) { return -1; }
int i = 0;
while (i < s.length() / 2 && s.charAt(i) == s.charAt(s.length() - 1 - i)) {
i++;
}
String left = removeIndex(s, i);
String right = removeIndex(s, s.length() - 1 - i);
if (isPalindrom(left)) { return i; }
if (isPalindrom(right)) { return s.length() - 1 - i; }
return -1;
}
static String removeIndex(String s, int index) {
String left = s.substring(0, index);
String right = s.substring(index + 1, s.length());
return left + right;
}
static boolean isPalindrom(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String s = scanner.nextLine();
int result = palindromeIndex(s);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}