I debugged my solution and it seems to work correkt, but does not pass.
Please help. Where is my mistake?
package com.codegym.task.task22.task2207;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/*
Inverted words
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String fileName = buf.readLine();
buf.close();
BufferedReader br = new BufferedReader(new FileReader(fileName));
StringBuilder sb = new StringBuilder();
while (br.ready()) {
sb.append(br.readLine());
sb.append(" ");
}
br.close();
String fileContent = sb.toString();
// System.out.println("FileContent: " + fileContent);
String words [] = fileContent.split(" ");
ArrayList<String> arrayList = new ArrayList(Arrays.asList(words));
int listSize = arrayList.size();
for (int i=0; i<listSize; i++) {
String first = arrayList.get(i);
String reverseFirst = reverse(first);
if (arrayList.contains(reverseFirst)) {
Pair pair = new Pair();
pair.first = first;
pair.second = reverseFirst;
result.add(pair);
arrayList.remove(reverseFirst);
listSize--;
}
arrayList.remove(i);
listSize--;
}
/**
for (Pair p : result) {
System.out.println(p.first + " " + p.second);
}
**/
}
public static String reverse(String str) {
return (new StringBuilder(str).reverse().toString());
}
public static class Pair {
String first;
String second;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (first != null ? !first.equals(pair.first) : pair.first != null) return false;
return second != null ? second.equals(pair.second) : pair.second == null;
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
@Override
public String toString() {
return first == null && second == null ? "" :
first == null ? second :
second == null ? first :
first.compareTo(second) < 0 ? first + " " + second : second + " " + first;
}
}
}