I am trying to find the number of string pairs in an array that are palindromes.
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
int count = 0;
String s[] = new String[N];
for (int i = 0; i < N; i++) {
s[i] = br.readLine();
}
for (int i = 0; i < N; i++) {
String str = s[i];
for(int j=i; j<N; j++) {
StringBuilder sb = new StringBuilder(s[j]);
if(str.equals(sb.reverse().toString())){
count++;
}
}
}
System.out.print(count);
}
}
Is there a more efficient way to do this?