-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPalindromeIndex.java
37 lines (32 loc) · 1.17 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
// https://www.hackerrank.com/challenges/palindrome-index/problem
package strings;
import java.util.Scanner;
public class PalindromeIndex {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
while (queries-- > 0) {
String string = scanner.next();
System.out.println(palindromicIndex(string));
}
}
private static int palindromicIndex(String string) {
for (int index = 0 ; index < (string.length() + 1) / 2 ; index++) {
if (string.charAt(index) != string.charAt(string.length() - 1 - index)) {
if (isPalindrome(string.substring(0, index) + string.substring(index + 1))) {
return index;
}
return string.length() - 1 - index;
}
}
return -1;
}
private static boolean isPalindrome(String string) {
for (int index = 0 ; index < string.length() / 2 ; index++) {
if (string.charAt(index) != string.charAt(string.length() - 1 - index)) {
return false;
}
}
return true;
}
}