-
Notifications
You must be signed in to change notification settings - Fork 0
/
PalindromeIndex.java
42 lines (37 loc) · 1.08 KB
/
PalindromeIndex.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
// https://www.hackerrank.com/challenges/palindrome-index
import java.io.*;
public class PalindromeIndex
{
public static void main(String[] args) throws IOException
{
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(byte T = Byte.parseByte(br.readLine()); T > 0; --T)
{
final char[] C = br.readLine().toCharArray();
final int N = C.length;
int c = -1;
for(int i = 0, j = N-1; i < j; ++i, --j)
{
if (C[i] != C[j])
{
c = isPalindrome(C, i+1, j+1) ? i : j;
break;
}
}
sb.append(c + "\n");
}
System.out.print(sb);
}
private static boolean isPalindrome(final char[] C, final int A, final int B)
{
for(int i = A, j = B-1; i < j; ++i, --j)
{
if (C[i] != C[j])
{
return false;
}
}
return true;
}
}