Answer:
Program.java
import java.util.Scanner;
public class Program {
public static boolean isPalindrome(String str){
int start = 0, end = str.length()-1;
while (start < end){
if(str.charAt(start) != str.charAt(end)){
return false;
}
start++;
end--;
}
return true;
}
public static int countPalindromePairs(String[] inarr){
int count = 0;
for(int i=0; i<inarr.length; i++){
for(int j=i+1; j<inarr.length; j++){
StringBuilder sb = new StringBuilder();
sb.append(inarr[i]).append(inarr[j]);
if(isPalindrome(sb.toString())){
count++;
}
}
}
return count == 0 ? -1 : count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.next();
String[] inarr = line.split(",");
int count = countPalindromePairs(inarr);
System.out.println("RESULT: "+count);
}
}
Explanation:
OUTPUT: